iPhone發送接收Http請求——ASIHttpRequest

ASIHttpRequest開源包,封裝iPhone/iPad上發送接收Http請求,官網地址 http://allseeing-i.com/ASIHTTPRequest/
 
支持功能:
1. 下載的數據直接保存到內存或文件系統裏
2. 提供直接提交(HTTP POST)文件的API
3. 能夠直接訪問與修改HTTP請求與響應HEADER
4. 輕鬆獲取上傳與下載的進度信息
5. 異步請求與隊列,自動管理上傳與下載隊列管理機
6. 認證與受權的支持
7. Cookie
8. 請求與響應的GZIP
9. 代理請求
 
使用方式:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
添加如下文件到項目(若請求只須要哪些文件也可選擇添加)
ASIHTTPRequestConfig.h
ASIHTTPRequestDelegate.h
ASIProgressDelegate.h
ASICacheDelegate.h
ASIHTTPRequest.h
ASIHTTPRequest.m
ASIDataCompressor.h
ASIDataCompressor.m
ASIDataDecompressor.h
ASIDataDecompressor.m
ASIFormDataRequest.h
ASIInputStream.h
ASIInputStream.m
ASIFormDataRequest.m
ASINetworkQueue.h
ASINetworkQueue.m
ASIDownloadCache.h
ASIDownloadCache.m
iPhone projects must also include:
ASIAuthenticationDialog.h
ASIAuthenticationDialog.m
Reachability.h (in the External/Reachability folder)
Reachability.m (in the External/Reachability folder)
CFNetwork.framework,
SystemConfiguration.framework,
 MobileCoreServices.framework,
CoreGraphics.framework,
libz.1.2.3.dylib
 

同步請求代碼
- (IBAction)grabURL:(id)sender
{
  NSURL *url = [NSURL URLWithString:@" http://allseeing-i.com"];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
    NSString *response = [request responseString];
  }
}

異步請求代碼
//發起請求,
- (void)newOrderShowRequest:(NSString *)requestInfo {
 NSURL *url = [NSURL URLWithString:newOrderShowLink];
 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 //設置處理返回結果代理函數,不設置則默認爲requestFinished
 [request setDidFinishSelector:@selector(newOrderShowRequestFinished:)];
 //設置處理返回錯誤代理函數,不設置則默認爲requestFailed
 [request setDidFailSelector:@selector(newOrderShowRequestFailed:)]; 
 [request setDelegate:self];
 [request startAsynchronous];
}
//處理返回結果
- (void)newOrderShowRequestFinished:(ASIHTTPRequest *)request {
   NSString *responseString = [request responseString];
   NSData *responseData = [request responseData];
}
//處理返回錯誤
- (void)newOrderShowRequestFailed:(ASIHTTPRequest *)request {
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"鏈接失敗" message:@"請檢查網絡鏈接." delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
 [alert show];
 [alert release];
}
 
經過設置處理返回結果/錯誤代理函數便可處理同時發起的多個請求,若是須要更規範的管理髮起的多個請求能夠使用隊列,代碼詳見官方例子
相關文章
相關標籤/搜索