偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

iPhone應(yīng)用中HTTP包裝開(kāi)源項(xiàng)目ASIHTTPRequest詳解

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

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

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

提供了以下:

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

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

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

可以訪問(wèn)和修改http請(qǐng)求和響應(yīng)header

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

異步請(qǐng)求和隊(duì)列,自動(dòng)管理上傳下載隊(duì)列機(jī)制

cookie 支持

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

代理請(qǐng)求

ASIHTTPRequest設(shè)置

在iphone 項(xiàng)目中使用ASIHTTPRequest

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

  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項(xiàng)目還必須包含以下文件

  1. ASIAuthenticationDialog.h  
  2. ASIAuthenticationDialog.m 

一個(gè)版本的Reachability類

添加必要的框架到項(xiàng)目中去

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

配置Reachability

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

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

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

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

在mac ox x項(xiàng)目中使用AHIHTTPRequest

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

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

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

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

特色功能如下:

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

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

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

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

5、異步請(qǐng)求與隊(duì)列,自動(dòng)管理上傳與下載隊(duì)列管理機(jī)

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

7、Cookie

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

9、代理請(qǐng)求

下面來(lái)兩個(gè)小例子:

  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)你需要添加更多的請(qǐng)求信息時(shí),如,添加個(gè)請(qǐng)求Header:

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

添加Post請(qǐng)求時(shí)的健值:

  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)帳號(hào):

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

一個(gè)異步請(qǐng)求:

  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ù)獲取的過(guò)程中,如果數(shù)據(jù)源復(fù)雜,一個(gè)請(qǐng)求隊(duì)列是必不可少的:

  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包裝開(kāi)源項(xiàng)目ASIHTTPRequest詳解的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

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

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-09 14:08:51

iPhoneHTTP請(qǐng)求協(xié)議

2011-08-22 10:06:38

IOS開(kāi)發(fā)ASIHTTPRequHTTP 請(qǐng)求

2011-08-12 13:35:23

iPhone文件流ASIHTTPRequ

2011-08-02 17:27:06

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

2011-08-15 11:37:20

iPhone開(kāi)發(fā)Mask

2011-08-12 14:33:06

iPhone緩存文件

2011-08-17 10:16:35

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

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-17 15:10:21

iPhone開(kāi)發(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開(kāi)發(fā)視圖

2011-08-17 15:19:38

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

2011-08-02 17:14:41

iPhone應(yīng)用 UITableVie

2009-03-05 18:20:58

開(kāi)源項(xiàng)目IphoneAndroid
點(diǎn)贊
收藏

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