ASINetworkQueues(经典2)

ASINetworkQueues, 它的delegate提供更为丰富的功能

提 供的更多的回调方法如下: a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据业务选择性的设置request对象的 deleaget。 b,requestDidReceiveResponseHeadersSelector,当接受完响应的Header后设计此方法,这个对下载大数据的 时候相当有用,你可以在方法里做更多业务上的处理。 c,requestDidFinishSelector,请求并响应成功完成时调用此方法 d,requestDidFailSelector,请求失败 e,queueDidFinishSelector,整个队列里的所有请求都结束时调用此方法。

它是NSOperationQueues的扩展,小而强大。但也与它的父类略有区别。如,仅添加到队列中其实并不能执行请求,只有调用[ queue g o]才会执行;一个正在运行中的队列,并不需要重复调用[ queue go ]。

默认情况下,队列中的一个请求如果失败,它会取消所有未完成的请求。可以设置[ queue setShouldCancelAllRequestsOnFailure:NO ]来修 正。

取消异步请求

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

取消的请求默认都会按请求失败处理,并调用请求失败delegate。 如果不想调用delegate方法,则设置:[ request clearDelegatesAndCancel];

队 列请求中需要注意的是,如果你取消了一个请求,队列会自动取消其它所有请求。 如果只想取消一个请求,可以设置队列:[ queue setShouldCancelAllRequestsOnFailure:NO ]; 如果想明确取消所有请求:[ queue cancelAllOperations ];

安全的内存回收建议

request并没有retain你的delegate,所以在没有请求完的时候释放了此delegate,需要在dealloc方法里先取消所有请求,再释放请求实例,如:

- (void)dealloc
{
   [request clearDelegatesAndCancel];
   [request release];
   ...
   [superdealloc];
}
向服务器端上传数据

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的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
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]);
 
posted @ 2015-09-23 09:10  fengsh_h  阅读(325)  评论(0编辑  收藏  举报