iOS的ASIHTTPRequest学习(2)

来源:互联网 发布:编程语言排行 编辑:程序博客网 时间:2024/05/16 12:53

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方法里先取消所有请求,再释放请求实例,如:

  1. - (void)dealloc 
  2.    [request clearDelegatesAndCancel]; 
  3.    [request release]; 
  4.    ... 
  5.    [super dealloc]; 
向服务器端上传数据

ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自动识别。
没有文件:application/x-www-form-urlencoded
有文件:multipart/form-data

  1. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
  2. [request setPostValue:@"Ben" forKey:@"first_name"]; 
  3. [request setPostValue:@"Copsey" forKey:@"last_name"]; 
  4. [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 
  5. [request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg"forKey:@"photos"]; 

如果要发送自定义数据:

  1. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  2. [request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]]; 
  3. // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody: 
  4. [request setRequestMethod:@"PUT"]; 
  5.  
  6. //用户自定义数据   字典类型  (可选) 
  7. request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"]; 
  8.  
  9. //post的数据 
  10. [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];
下载文件

通过设置request的setDownloadDestinationPath,可以设置下载文件用的下载目标目录。
首先,下载过程文件会保存在temporaryFileDownloadPath目录下。如果下载完成会做以下事情:
1,如果数据是压缩的,进行解压,并把文件放在downloadDestinationPath目录中,临时文件被删除
2,如果下载失败,临时文件被直接移到downloadDestinationPath目录,并替换同名文件。

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

获取响应信息

信息:status , header, responseEncoding

  1. [request responseStatusCode]; 
  2. [[request responseHeaders] objectForKey:@"X-Powered-By"]; 
  3.  [request responseEncoding]; 
获取请求进度

有两个回调方法可以获取请求进度,
1,downloadProgressDelegate,可以获取下载进度
2,uploadProgressDelegate,可以获取上传进度

cookie的支持

如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。
你可以用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有Cookies。
当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:

  1. //Create a cookie 
  2. NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease]; 
  3. [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue]; 
  4. [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName]; 
  5. [properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain]; 
  6. [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires]; 
  7. [properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath]; 
  8. NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease]; 
  9.   
  10. //This url will return the value of the 'ASIHTTPRequestTestCookie' cookie 
  11. url = [NSURL URLWithString:@" http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"]; 
  12. request = [ASIHTTPRequest requestWithURL:url]; 
  13. [request setUseCookiePersistence:NO]; 
  14. [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]]; 
  15. [request startSynchronous]; 
  16.   
  17. //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie' 
  18. NSLog(@"%@",[request responseString]); 
0 0