利用ASIHTTPRequest访问网络

ASIHTTPRequest是第三方类库,ASIHTTPRequest对CFNetwork API进行了封装。

有如下特点:

  • l 通过简单的接口,即可完成向服务端提交数据和从服务端获取数据的工作
  • l 下载的数据,可存储到内存中或直接存储到磁盘中
  • l 能上传本地文件到服务端
  • l 可以方便的访问和操作请求和返回的Http头信息
  • l 可以获取到上传或下载的进度信息,为应用程序提供更好的体验
  • l 支持上传或下载队列,并且可获取队列的进度信息
  • l 支持基本、摘要和NTLM身份认证,在同一会话中授权凭证会自动维持,并且可以存储在Keychain(Mac和iOS操作系统的密码管理系统)中
  • l 支持Cookie
  • l 当应用(iOS 4+)在后台运行时,请求可以继续运行
  • l 支持GZIP压缩数据
  • l 内置的ASIDownloadCache类,可以缓存请求返回的数据,这样即使没有网络也可以返回已经缓存的数据结果
  • l ASIWebPageRequest –可以下载完整的网页,包括包含的网页、样式表、脚本等资源文件,并显示在UIWebView /WebView中。任意大小的页面都可以无限期缓存,这样即使没有网络也可以离线浏览
  • l 支持客户端证书
  • l 支持通过代理发起Http请求
  • l 支持带宽限制。在iOS平台,可以根据当前网络情况来自动决定是否限制带宽,例如当使用WWAN(GPRS/Edge/3G)网络时限制,而当使用WIFI时不做任何限制
  • l 支持断点续传
  • l 支持同步和异步请求

第一、同步请求:

 


 1 ASIHTTPRequest *httpRequest=[ASIHTTPRequest  requestWithURL:url];
 2        [httpRequest setRequestMethod:@"GET"];
 3        [httpRequest setTimeOutSeconds:60];
 4       [httpRequest startSynchronous];
 5     NSError *error=httpRequest.error;
 6         if(error==nil)
 7         {
 8             NSData *data=httpRequest.responseData;
 9             UIImage *image=[UIImage imageWithData:data];
10             self.image=image;
11         }else
12         {
13            NSLog(@"请求网络错误");
14         }




第二、异步请求:

 

用delegate实现:

 


-(void)asynchronous:(NSURL*)url
{
    ASIHTTPRequest *httpRequest=[ASIHTTPRequest  requestWithURL:url];
    [httpRequest setRequestMethod:@"GET"];
    [httpRequest setTimeOutSeconds:60];
    
    httpRequest.delegate=self;
    [httpRequest startAsynchronous];

}
#pragma mark - ASIHTTPRequest delegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    UIImage *image=[UIImage imageWithData:request.responseData];
    self.image=image;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error=request.error;
    NSLog(@"请求出错:%@",error);
}


 


用block实现:


ASIHTTPRequest *httpRequest=[ASIHTTPRequest  requestWithURL:url];
    [httpRequest setRequestMethod:@"GET"];
    [httpRequest setTimeOutSeconds:60];
    
    //httpRequest.delegate=self;
  [httpRequest setCompletionBlock:^{
        UIImage *image=[UIImage imageWithData:httpRequest.responseData];
        self.image=image; 
  }];
    [httpRequest setFailedBlock:^{
        NSError *error=httpRequest.error;
        NSLog(@"请求出错:%@",error);

    }];
    [httpRequest startAsynchronous];


 


Block 回调:


- (void)setStartedBlock:(ASIBasicBlock)aStartedBlock;
- (void)setHeadersReceivedBlock:(ASIHeadersBlock)aReceivedBlock;
- (void)setCompletionBlock:(ASIBasicBlock)aCompletionBlock;
- (void)setFailedBlock:(ASIBasicBlock)aFailedBlock;
- (void)setBytesReceivedBlock:(ASIProgressBlock)aBytesReceivedBlock;
- (void)setBytesSentBlock:(ASIProgressBlock)aBytesSentBlock;
- (void)setDownloadSizeIncrementedBlock:(ASISizeBlock) aDownloadSizeIncrementedBlock;
- (void)setUploadSizeIncrementedBlock:(ASISizeBlock) anUploadSizeIncrementedBlock;
- (void)setDataReceivedBlock:(ASIDataBlock)aReceivedBlock;


 

 

第四、缓存策略

 

   

NSString *cathPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

   ASIDownloadCache *cache=[[ASIDownloadCache alloc]init];

   [cache setStoragePath:cathPath];

    cache.defaultCachePolicy=ASIOnlyLoadIfNotCachedCachePolicy;

 

 

 

   

//持久缓存,一直保存在本地

  httpRequest.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;

httpRequest.downloadCache=cache;

    [httpRequest startAsynchronous];

      //监听数据的来源

      if (httpRequest.didUseCachedResponse) {

          NSLog(@"data is from cache");

      }else

      {

          NSLog(@"data is form net");

      }

 

 

 

原文地址:https://www.cnblogs.com/liuziyu/p/4311045.html