您的位置:首页 > 理论基础 > 计算机网络

iOS的ASIHTTPRequest学习(2)

2014-09-23 14:35 357 查看
ASIHTTPRequest是一款极其强劲的HTTP访问开源项目。让简单的API完成复杂的功能,如:异步请求,队列请求,GZIP压缩,缓存,断点续传,进度跟踪,上传文件,HTTP认证在新的版本中,还加入了Objective-C闭包Block的支持,让我们的代码更加轻简灵活。

AD:2014WOT全球软件技术峰会北京站 课程视频发布

取消异步请求

首先,同步请求是不能取消的。

其次,不管是队列请求,还是简单的异步请求,全部调用[ request cancel ]来取消请求。

取消的请求默认都会按请求失败处理,并调用请求失败delegate。

如果不想调用delegate方法,则设置:[ request clearDelegatesAndCancel];
队列请求中需要注意的是,如果你取消了一个请求,队列会自动取消其它所有请求。

如果只想取消一个请求,可以设置队列:[ queue setShouldCancelAllRequestsOnFailure:NO ];

如果想明确取消所有请求:[ queue cancelAllOperations ];
安全的内存回收建议

request并没有retain你的delegate,所以在没有请求完的时候释放了此delegate,需要在dealloc方法里先取消所有请求,再释放请求实例,如:
- (void)dealloc
{
[request clearDelegatesAndCancel];
[request release];
...
[super dealloc];
}

向服务器端上传数据

ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自动识别。

没有文件:application/x-www-form-urlencoded

有文件:multipart/form-data
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg"forKey:@"photos"];


如果要发送自定义数据:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@"PUT"];

//用户自定义数据   字典类型  (可选)
request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"];

//post的数据
[request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

下载文件

通过设置request的setDownloadDestinationPath,可以设置下载文件用的下载目标目录。

首先,下载过程文件会保存在temporaryFileDownloadPath目录下。如果下载完成会做以下事情:

1,如果数据是压缩的,进行解压,并把文件放在downloadDestinationPath目录中,临时文件被删除

2,如果下载失败,临时文件被直接移到downloadDestinationPath目录,并替换同名文件。

如果你想获取下载中的所有数据,可以实现delegate中的request:didReceiveData:方法。但如果你实现了这个方法,request在下载完后,request并不把文件放在downloadDestinationPath中,需要手工处理。
获取响应信息

信息:status , header, responseEncoding
[request responseStatusCode];
[[request responseHeaders] objectForKey:@"X-Powered-By"];
[request responseEncoding];

获取请求进度

有两个回调方法可以获取请求进度,

1,downloadProgressDelegate,可以获取下载进度

2,uploadProgressDelegate,可以获取上传进度
cookie的支持

如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。

你可以用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有Cookies。

当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
//Create a cookie
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
[properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
[properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];

//This url will return the value of the 'ASIHTTPRequestTestCookie' cookie
url = [NSURL URLWithString:@" http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"]; request = [ASIHTTPRequest requestWithURL:url];
[request setUseCookiePersistence:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
[request startSynchronous];

//Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
NSLog(@"%@",[request responseString]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: