object-c的http post请求之 ASIFormDataRequest使用

ASIHTTPRequest类库中的ASIFormDataRequest是实现HTTP协议中的处理POST表单的很好的类库。使用起来非常简单。

在说明之前先需要了解HTTP请求的Get和Post方法。
Get方法一般是从服务器获取数据,而Post方法主要是向服务器传输一些数据。
Get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到完整的URL请求。ASIHTTPRequest类主要完成Get请求。
Post方法是将表单内各个字段与其内容放置在HTML HEADER内传送到ACTION属性所指的URL地址。用户看不到这个过程。ASIFormDataRequest类主要完成Post请求。
关于ASIHTTPRequest类库的安装配置详见《ASIHTTPRequest的环境配置和使用示例
1,构造请求
需要包含头文件:#import "ASIFormDataRequest.h"
ASIFormDataRequest *request; request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.tekuba.net"]]; //构造请求 
[request setPostValue:emailFiled.text forKey:@"Mail"]; [request setPostValue:accountFiled.text forKey:@"UserAccount"];//帐户
[request setPostValue:[self md5:passwordFiled.text] forKey:@"PassWord"];//密码
[request setDelegate:self]; //配置代理为本类
[request setTimeOutSeconds:10]; //设置超时 
[request setDidFailSelector:@selector(urlRequestFailed:)]; [request setDidFinishSelector:@selector(urlRequestSucceeded:)]; [request startSynchronous];//同步传输 //[request startAsynchronous];//异步传输
2,实现数据处理方法 //连接失败
-(void)urlRequestFailed:(ASIHTTPRequest *)request { NSError *error =[request error]; NSLog(@"%@",error); NSLog(@"连接失败!"); UIAlertView * alt=[[UIAlertView alloc] initWithTitle:@"提示" message:@"连接失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alt show]; [alt release]; } //请求成功
-(void)urlRequestSucceeded:(ASIHTTPRequest *)request { NSData *data=[request responseData]; NSXMLParser *parser=[[NSXMLParser alloc] initWithData:data]; NSLog(@"data length = %d",[data length]); NSLog(@"xml data = %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); [parser setDelegate:self]; [parser parse];//进入解析
}

 

转载本文请注明来自特酷吧并注明本文链接,本文地址:www.tekuba.net/program/269/
推荐阅读:

 

 

实例:

IPhone之ASIFormDataRequest POST操作架构设计

//开启iphone网络开关 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:host]]; //超时时间 request.timeOutSeconds = 30; //定义异步方法 [request setDelegate:self]; [request setDidFailSelector:@selector(requestDidFailed:)]; [request setDidFinishSelector:@selector(requestDidSuccess:)]; //用户自定义数据 字典类型 (可选) request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"]; //post的数据 [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]]; //开始执行 [request startAsynchronous]; //执行成功 - (void)requestDidSuccess:(ASIFormDataRequest *)request { //获取头文件 NSDictionary *headers = [request responseHeaders]; //获取http协议执行代码 NSLog(@"Code:%d",[request responseStatusCode]); if ([delegate respondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)]) { //执行委托操作 (架构设计 自选) [delegate OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers]; } //清空 if (request) { [request release]; } //关闭网络 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } //执行失败 - (void)requestDidFailed:(ASIFormDataRequest *)request{ //获取的用户自定义内容 NSString *method = [request.userInfo objectForKey:@"Method"]; //获取错误数据 NSError *error = [request error]; if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)]) { //执行委托 将错误数据传其他方式(架构设计 自选) [delegate OARequestFailed:method withError:error]; } if (request) { [request release]; } [UIApplication sharedApplication].networkActivityIndicatorVisible = NO } //执行成功函数 - (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers { NSString *responseStr = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; //服务返回post后的数据 NSLog(@"response:\n%@",responseStr); } //执行失败函数 - (void)OARequestFailed:(NSString *)method withError:(NSError *)error { NSLog(@"Error:%@",error); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败, 请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil]; [alert show]; [alert release]; } 

  

你可能感兴趣的:(request)