自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone應(yīng)用中HTTP包裝開源項目ASIHTTPRequest詳解

移動開發(fā) iOS
ASIHTTPRequest是簡單易用的,它封裝了CFNetwork API。使得與Web服務(wù)器通信變得更簡單。它是用Objective-C編寫的,可以在MAC OS X和iPhone應(yīng)用中使用。

ASIHTTPRequest一個強(qiáng)大的HTTP包裝開源項目是本文要介紹的內(nèi)容,ASIHTTPRequest是什么?ASIHTTPRequest是簡單易用的,它封裝了CFNetwork API。使得與Web服務(wù)器通信變得更簡單。它是用Objective-C編寫的,可以在MAC OS X和iPhone應(yīng)用中使用。

它適用于執(zhí)行基本的HTTP請求和互動(或者說是反饋)。ASIFormDataRequest子類可以簡單的實現(xiàn)提交數(shù)據(jù)和文件。使用multipart/form-data

提供了以下:

一個從web服務(wù)器提交和獲取數(shù)據(jù)的接口

直接下載數(shù)據(jù)到內(nèi)存或者本地文件系統(tǒng)里

能夠從本地提交文件,作為post數(shù)據(jù)的一部分。兼容HTML file input mechanism

可以訪問和修改http請求和響應(yīng)header

獲得上傳下載的進(jìn)度信息

異步請求和隊列,自動管理上傳下載隊列機(jī)制

cookie 支持

請求和響應(yīng)的gzip支持

代理請求

ASIHTTPRequest設(shè)置

在iphone 項目中使用ASIHTTPRequest

1、添加一些必要的文件,復(fù)制以下文件到項目中去

  1. ASIHTTPRquestConfig.h  
  2. ASInputStream.h  
  3. ASInputStream.m  
  4. ASIHTTPRequest.h  
  5. ASIHTTPRequest.h  
  6. ASINSStringAdditions.h  
  7. ASINSStringAdditions.m  
  8. ASIFormDataRequest.h  
  9. ASIFormDataRequest.m  
  10. ASINetworkQueue.h  
  11. ASINetworkQueue.m 

iphone項目還必須包含以下文件

  1. ASIAuthenticationDialog.h  
  2. ASIAuthenticationDialog.m 

一個版本的Reachability類

添加必要的框架到項目中去

  1. CFNetwork.framework  
  2. SystemConfiguration.framework  
  3. libz.1.2.3.dylib 

配置Reachability

在iphone上,ASIHTTPRequest使用Apple的Reachability類。

Reachability有兩個版本,他們都能在ASIHTTPRequest發(fā)行文件的Reachability文件夾中找到。

2.0版本是最新的辦迸。如果你的項目是基于iphone os 3.x和更新的系統(tǒng),你應(yīng)該使用2.0版本的。包括.h和.m文件。保證在ASIHTTPRequestConfig.h文件中REACHABILITY_20_API的值為1

1.5是個老版本,它和iphone os 2.2.1-iphone os 3.0兼容。保證在ASIHTTPRequestConfig.h文件中REACHABILITY_20_API的值為0

在mac ox x項目中使用AHIHTTPRequest

為了在Mac os x項目中使用ASIHTTPRequest,你需要導(dǎo)入以下:

  1. SystemConfiguration.framework + zlib  
  2. CoreService.framework 

在Mac OS X上,CFNetwork 是CoreServices框架的一部分。除非你寫的是基于控制臺的應(yīng)用程序,官方地址:http://allseeing-i.com/ASIHTTPRequest/

ASIHTTPRequest,是一個直接在CFNetwork上做的開源項目,提供了一個比官方更方便更強(qiáng)大的HTTP網(wǎng)絡(luò)傳輸?shù)姆庋b。

特色功能如下:

1、下載的數(shù)據(jù)直接保存到內(nèi)存或文件系統(tǒng)里

2、提供直接提交(HTTP POST)文件的API

3、可以直接訪問與修改HTTP請求與響應(yīng)HEADER

4、輕松獲取上傳與下載的進(jìn)度信息

5、異步請求與隊列,自動管理上傳與下載隊列管理機(jī)

6、認(rèn)證與授權(quán)的支持

7、Cookie

8、請求與響應(yīng)的GZIP

9、代理請求

下面來兩個小例子:

  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  2. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  3. [request start];  
  4. NSError *error = [request error];  
  5. if (!error) {  
  6. NSString *response = [request responseString];  

當(dāng)你需要添加更多的請求信息時,如,添加個請求Header:

  1. [request addRequestHeader:@"name" value:@"Jory lee"]; 

添加Post請求時的健值:

  1. [request setPostValue:@"Ben" forKey:@"first_name"];  
  2. [request setPostValue:@"Copsey" forKey:@"last_name"];  
  3. [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 

設(shè)置HTTP的授權(quán)帳號:

  1. [request setUsername:@"username"];  
  2. [request setPassword:@"password"]; 

一個異步請求:

  1. - (IBAction)grabURLInBackground:(id)sender  
  2. {  
  3. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5. [request setDelegate:self];  
  6. [request startAsynchronous];  
  7. }  
  8. - (void)requestFinished:(ASIHTTPRequest *)request  
  9. {  
  10. // Use when fetching text data  
  11. NSString *responseString = [request responseString];  
  12. // Use when fetching binary data  
  13. NSData *responseData = [request responseData];  
  14. }  
  15. - (void)requestFailed:(ASIHTTPRequest *)request  
  16. {  
  17. NSError *error = [request error];  
  18. }    

在我們數(shù)據(jù)獲取的過程中,如果數(shù)據(jù)源復(fù)雜,一個請求隊列是必不可少的:

  1. - (IBAction)grabURLInTheBackground:(id)sender  
  2. {  
  3. if (![self queue]) {  
  4. [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];  
  5. }  
  6. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  7. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  8. [request setDelegate:self];  
  9. [request setDidFinishSelector:@selector(requestDone:)];  
  10. [request setDidFailSelector:@selector(requestWentWrong:)];  
  11. [[self queue] addOperation:request]; //queue is an NSOperationQueue  
  12. }  
  13. - (void)requestDone:(ASIHTTPRequest *)request  
  14. {  
  15. NSString *response = [request responseString];  
  16. }  
  17. - (void)requestWentWrong:(ASIHTTPRequest *)request  
  18. {  
  19. NSError *error = [request error];  
  20. }    

小結(jié):iPhone應(yīng)用中HTTP包裝開源項目ASIHTTPRequest詳解的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-09 14:08:51

iPhoneHTTP請求協(xié)議

2011-08-22 10:06:38

IOS開發(fā)ASIHTTPRequHTTP 請求

2011-08-12 13:35:23

iPhone文件流ASIHTTPRequ

2011-08-02 17:27:06

iPhone應(yīng)用 剪切技巧

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 11:37:20

iPhone開發(fā)Mask

2011-08-17 10:16:35

iPhone應(yīng)用HTTP請求協(xié)議

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-17 15:10:21

iPhone開發(fā)Web視圖

2014-06-05 10:21:29

HTTP

2011-08-19 14:14:14

iPhone應(yīng)用

2011-07-27 11:14:37

iPhone UITableVie

2011-07-08 17:45:19

iPhone 文檔

2011-07-19 14:36:32

iPhone

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-08-12 10:04:24

iPhone開發(fā)視圖

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2009-03-05 18:20:58

開源項目IphoneAndroid

2011-08-02 17:14:41

iPhone應(yīng)用 UITableVie
點贊
收藏

51CTO技術(shù)棧公眾號