強大的HTTP包裝開源項目ASIHTTPRequest介紹

ASIHTTPRequest 是一個直接在CFNetwork上作的開源項目,提供了一個比官方更方便更強大的HTTP網絡傳輸的封裝。它的特點功能以下:網絡

1,下載的數據直接保存到內存或文件系統裏app

2,提供直接提交(HTTP POST)文件的API異步

3,能夠直接訪問與修改HTTP請求與響應HEADERpost

4,輕鬆獲取上傳與下載的進度信息fetch

5,異步請求與隊列,自動管理上傳與下載隊列管理機url

6,認證與受權的支持spa

7,Cookie代理

8,請求與響應的GZIPcode

9,代理請求orm

 源碼:http://www.jinhusns.com/Products/Download/?type=xcj

 下面來兩個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 的確省了不少代碼。

相關文章
相關標籤/搜索