ASIHTTPRequest框架

github
关于ASIHttpRequest

配置

  1. 拷贝Classes除Tests外所有文件,
  2. 拷贝External文件
  3. 全部设置手动管理 -fno-objc-arc

添加其他框架:

  • CFNetwork.framework
  • libz.dylib
  • libxml2.dylib
  • SystemConfiguration.framework
  • SystemConfiguration.framework
  • CoreGraphics.framework

设置头文件搜索路径Build Settings —> Search Paths—>Header Search Paths:
${SDK_DIR}/usr/include/libxml2

NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  //get请求
   request.delegate = self;
   [request startAsynchronous];
       //如果要设置请求头
      request addRequestHeader:<#(NSString *)#> value:<#(NSString *)#>
   
   
   //POST请求
   ASIFormDataRequest  *dataRequest = [ASIFormDataRequest requestWithURL:url];
   dataRequest setPostValue:<#(id)#> forKey:<#(NSString *)#>
   
   //上传文件
       ASIFormDataRequest  *dataRequest = [ASIFormDataRequest requestWithURL:url];
   dataRequest setFile:<#(NSString *)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>
   dataRequest setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>
   
   //同步请求
   [dataRequest startAsynchronous];
   
    //异步请求 第一种:代理模式
//    dataRequest.delegate = self;
//    [dataRequest startSynchronous];
   
   //异步请求 第二章:方法体回调
   [dataRequest setCompletionBlock:^{
       
   }];
   [dataRequest setFailedBlock:^{
       
   }];
}
//代理
- (void)requestFinished:(ASIHTTPRequest *)request{
   NSLog(@"success:%@",request.responseString);
}
- (void)requestFailed:(ASIHTTPRequest *)request{
   NSLog(@"failed:%d",request.responseStatusCode);
}

使用NSOperationQueue管理请求

使用NSOerationQueue 来管理ASIHTTPRequest 时,无需调用 startSyn…..直接将ASIHTTPRequest对象添加到队列中。

if(!_queue)
   {
       _queue  =[[NSOperationQueue alloc]init];
   }
   //为request 设置自定义回调处理方法
   [request setDidFinishSelector:@selector(requestDone:)];
   [request setDidFailSelector:@selector(requestWrong:)];
request.delegate =self;
   [_queue addOperation:request];    
-(void)Finish:(ASIHTTPRequest *)request{
   NSLog(@"%@",request.responseString);
}
-(void)Fail:(ASIHTTPRequest *)request{
   
}

UIWebView 离线化存储

手册


web = [[UIWebView alloc]init];
   web.frame = CGRectMake(0, 0, 200, 300);
   [self.view addSubview:web];
   _queue = [[NSOperationQueue alloc]init];
   NSURL *url = [NSURL URLWithString:@"http://www.imooc.com"];
   ASIWebPageRequest *webPage = [[ASIWebPageRequest  alloc]initWithURL:url];
   
   [webPage setDidFinishSelector:@selector(Finish:)];
   [webPage setDidFailSelector:@selector(Fail:)];
   webPage.delegate =self;
//设置永久存储
   [webPage setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
   //设置缓存策略
   [webPage setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy|ASIAskServerIfModifiedCachePolicy];
   [webPage setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
   [webPage setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:webPage]];
   
   [webPage setDownloadCache:[ASIDownloadCache sharedCache]];
   NSString *home=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   NSLog(@"%@",home);
   [_queue addOperation:webPage];
}
-(void)Finish:(ASIHTTPRequest *)request{
   NSString *response = [NSString stringWithContentsOfFile:[request downloadDestinationPath] encoding:[request responseEncoding] error:nil];
   [web loadHTMLString:response baseURL:nil];
   NSLog(@"%@",response);
}
-(void)Fail:(ASIHTTPRequest *)request{
   NSLog(@"%@",request.error);
}

你可能感兴趣的:(ASIHTTPRequest框架)