iPhone應(yīng)用中HTTP包裝開源項目ASIHTTPRequest詳解
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ù)制以下文件到項目中去
- ASIHTTPRquestConfig.h
- ASInputStream.h
- ASInputStream.m
- ASIHTTPRequest.h
- ASIHTTPRequest.h
- ASINSStringAdditions.h
- ASINSStringAdditions.m
- ASIFormDataRequest.h
- ASIFormDataRequest.m
- ASINetworkQueue.h
- ASINetworkQueue.m
iphone項目還必須包含以下文件
- ASIAuthenticationDialog.h
- ASIAuthenticationDialog.m
一個版本的Reachability類
添加必要的框架到項目中去
- CFNetwork.framework
- SystemConfiguration.framework
- 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)入以下:
- SystemConfiguration.framework + zlib
- 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、代理請求
下面來兩個小例子:
- NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request start];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- }
當(dāng)你需要添加更多的請求信息時,如,添加個請求Header:
- [request addRequestHeader:@"name" value:@"Jory lee"];
添加Post請求時的健值:
- [request setPostValue:@"Ben" forKey:@"first_name"];
- [request setPostValue:@"Copsey" forKey:@"last_name"];
- [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
設(shè)置HTTP的授權(quán)帳號:
- [request setUsername:@"username"];
- [request setPassword:@"password"];
一個異步請求:
- - (IBAction)grabURLInBackground:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request startAsynchronous];
- }
- - (void)requestFinished:(ASIHTTPRequest *)request
- {
- // Use when fetching text data
- NSString *responseString = [request responseString];
- // Use when fetching binary data
- NSData *responseData = [request responseData];
- }
- - (void)requestFailed:(ASIHTTPRequest *)request
- {
- NSError *error = [request error];
- }
在我們數(shù)據(jù)獲取的過程中,如果數(shù)據(jù)源復(fù)雜,一個請求隊列是必不可少的:
- - (IBAction)grabURLInTheBackground:(id)sender
- {
- if (![self queue]) {
- [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
- }
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request setDidFinishSelector:@selector(requestDone:)];
- [request setDidFailSelector:@selector(requestWentWrong:)];
- [[self queue] addOperation:request]; //queue is an NSOperationQueue
- }
- - (void)requestDone:(ASIHTTPRequest *)request
- {
- NSString *response = [request responseString];
- }
- - (void)requestWentWrong:(ASIHTTPRequest *)request
- {
- NSError *error = [request error];
- }
小結(jié):iPhone應(yīng)用中HTTP包裝開源項目ASIHTTPRequest詳解的內(nèi)容介紹完了,希望本文對你有所幫助!