強大的HTTP封裝開源項目ASIHTTPRequest介紹強大的HTTP封裝開源項目ASIHTTPRequest介紹
ASIHTTPRequest 是一個直接在CFNetwork上做的開源項目,提供了一個比官方更方便更強大的HTTP網路傳輸的封裝。它的特色功能如下:
1,下載的資料直接儲存到記憶體或檔案系統裡
2,提供直接提交(HTTP POST)檔案的API
3,可以直接存取與修改HTTP請求與響應HEADER
4,輕鬆擷取上傳與下載的進度資訊
5,非同步請求與隊列,自動管理上傳與下載隊列管理機
6,認證與授權的支援
7,Cookie
8,請求與響應的GZIP
9,代理請求
下面來兩個ASIHTTPRequest的小例子:
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];
}
當你需要添加更多的請求資訊時,如,添加個請求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"];
設定HTTP的授權帳號:
[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];
}
在我們資料擷取的過程中,如果資料來源複雜,一個請求隊列是必不可少的:
- (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];
}
另外大家可以通過下面這個代碼例子,看 ASIHTTP 比傳統的 post 方法的方便之處
post用法
NSString *post = @"這裡放要傳遞的參數";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"請求地址"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//[request addValue:@"gzip" forHTTPHeaderField:@"Accepts-Encoding"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedbData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
ASIHTTP 方法
NSURL *url = [NSURL URLWithString:@"請求地址"];
//ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"值" forKey:@"參數1"];
[request setPostValue:@"值" forKey:@"參數2"];
[request start];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(response);
}
相比之下,ASIHTTP 的確省了很多代碼。更多資訊請訪問官方網站 http://allseeing-i.com/
出處:http://www.cocoachina.com/iphonedev/sdk/2010/1011/2159.html
#Iphone