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

iPhone應(yīng)用開發(fā)中ASIHTTPRequest詳解

移動開發(fā) iOS
本文介紹的是iPhone應(yīng)用開發(fā)中ASIHTTPRequest,ASIHTTPRequest 是一款極其強(qiáng)勁的 HTTP 訪問開源項(xiàng)目。讓簡單的 API 完成復(fù)雜的功能,

iPhone應(yīng)用開發(fā)中ASIHTTPRequest詳解是本文要講解的內(nèi)容,ASIHTTPRequest 是一款極其強(qiáng)勁的 HTTP 訪問開源項(xiàng)目。讓簡單的 API 完成復(fù)雜的功能,如:異步請求,隊(duì)列請求,GZIP 壓縮,緩存,斷點(diǎn)續(xù)傳,進(jìn)度跟蹤,上傳文件,HTTP 認(rèn)證。在新的版本中,還加入了 Objective-C 閉包 Block 的支持,讓我們的代碼加輕簡靈活。

下面就舉例說明它的 API 用法。

發(fā)起一個同步請求

同步意為著線程阻塞,在主線程中使用此方法會使應(yīng)用Hang住而不響應(yīng)任何用戶事件。所以,在應(yīng)用程序設(shè)計(jì)時,大多被用在專門的子線程增加用戶體驗(yàn),或用異步請求代替(下面會講到)。

  1. - (IBAction)grabURL:(id)sender  
  2.  {  
  3.    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4.    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5.    [request startSynchronous];  
  6.    NSError *error = [request error];  
  7.    if (!error) {  
  8.      NSString *response = [request responseString];  
  9.    }  
  10.  } 

用 requestWithURL 快捷方法獲取 ASIHTTPRequest 的一個實(shí)例

startSynchronous 方法啟動同步訪問

由于是同步請求,沒有基于事件的回調(diào)方法,所以從 request的error 屬性獲取錯誤信息

responseString,為請求的返回 NSString 信息

創(chuàng)建一個異步請求

異步請求的好處是不阻塞當(dāng)前線程,但相對于同步請求略為復(fù)雜,至少要添加兩個回調(diào)方法來獲取異步事件。下面異步請求代碼完成上面同樣的一件事情:

  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];  

與上面不同的地方是指定了一個 "delegate",并用 startAsynchronous 來啟動網(wǎng)絡(luò)請求

在這里實(shí)現(xiàn)了兩個 delegate 的方法,當(dāng)數(shù)據(jù)請求成功時會調(diào)用 requestFinished,請求失敗時(如網(wǎng)絡(luò)問題或服務(wù)器內(nèi)部錯誤)會調(diào)用 requestFailed。

隊(duì)列請求

提供了一個對異步請求更加精準(zhǔn)豐富的控制。如:可以設(shè)置在隊(duì)列中同步請求的連接數(shù)。往隊(duì)列里添加的請求實(shí)例數(shù)大于 maxConcurrentOperationCount 時,請求實(shí)例將被置為等待,直到前面至少有一個請求完成并出列才被放到隊(duì)列里執(zhí)行。這也適用于當(dāng)我們有多個請求需求按順序執(zhí)行的時候(可能是業(yè)務(wù)上的需要,也可能是軟件上的調(diào)優(yōu)),僅僅需要把 maxConcurrentOperationCount 設(shè)為“1”。

  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];  

創(chuàng)建 NSOperationQueue,這個 Cocoa 架構(gòu)的執(zhí)行任務(wù)(NSOperation)的任務(wù)隊(duì)列。我們通過 ASIHTTPRequest.h 的源碼可以看到,此類本身就是一個 NSOperation 的子類。也就是說它可以直接被放到"任務(wù)隊(duì)列"中并被執(zhí)行。上面的代碼除了隊(duì)列的創(chuàng)建與添加操作外,其它代碼與上一例一樣。

隊(duì)列異步請求中中獲取或識別不同request小技巧

可以設(shè)置一個上下文(userInfo)到 request 對象中,當(dāng)請求響應(yīng)完后可以通過訪問 request 對象的 userInfo 獲取里面的信息

為每一個請求實(shí)例設(shè)置不同的 setDidFinishSelector / setDidFailSelector 的回調(diào)方法

子類化 ASIHTTPRequest,重寫 requestFinished: 與 failWithProblem: 方法

ASINetworkQueues, 它的delegate提供更為豐富的功能

提供的更多的回調(diào)方法如下:

  1. requestDidStartSelector,請求發(fā)起時會調(diào)此方法,你可以在此方法中跟據(jù)業(yè)務(wù)選擇性的設(shè)置 request 對象的 deleaget   
  2. requestDidReceiveResponseHeadersSelector,當(dāng)接受完響應(yīng)的 Header 后設(shè)計(jì)此方法,這個對下載大數(shù)據(jù)的時候相當(dāng)有用,你可以在方法里做更多業(yè)務(wù)上的處理   
  3. requestDidFinishSelector,請求并響應(yīng)成功完成時調(diào)用此方法   
  4. requestDidFailSelector,請求失敗   
  5. queueDidFinishSelector,整個隊(duì)列里的所有請求都結(jié)束時調(diào)用此方法  

它是 NSOperationQueues 的擴(kuò)展,小而強(qiáng)大。但也與它的父類略有區(qū)別。如,僅添加到隊(duì)列中其實(shí)并不能執(zhí)行請求,只有調(diào)用[  queue g o ]才會執(zhí)行;一個正在運(yùn)行中的隊(duì)列,并不需要重復(fù)調(diào)用[  queue go  ]。默認(rèn)情況下,隊(duì)列中的一個請求如果失敗,它會取消所有未完成的請求??梢栽O(shè)置[  queue setShouldCancelAllRequestsOnFailure:NO  ]來修正。

取消異步請求

首先,同步請求是不能取消的。

其次,不管是隊(duì)列請求,還是簡單的異步請求,全部調(diào)用[ request cancel ]來取消請求。取消的請求默認(rèn)都會按請求失敗處理,并調(diào)用請求失敗delegate。

如果不想調(diào)用delegate方法,則設(shè)置:[ request clearDelegatesAndCancel];

隊(duì)列請求中需要注意的是,如果你取消了一個請求,隊(duì)列會自動取消其它所有請求。如果只想取消一個請求,可以設(shè)置隊(duì)列:

  1. [ queue setShouldCancelAllRequestsOnFailure:NO ];  

如果想明確取消所有請求:[ queue cancelAllOperations ];

安全的內(nèi)存回收建議

request并沒有retain你的delegate,所以在沒有請求完的時候釋放了此delegate,需要在dealloc方法里先取消所有請求,再釋放請求實(shí)例,如:

  1.  - (void)dealloc  
  2. {  
  3.    [request clearDelegatesAndCancel];  
  4.    [request release];  
  5.    ...  
  6.    [super dealloc];  

向服務(wù)器端上傳數(shù)據(jù)

ASIFormDataRequest ,模擬 Form 表單提交,其提交格式與 Header 會自動識別。

沒有文件:application/x-www-form-urlencoded

有文件:multipart/form-data

  1.  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];  
  2. [request setPostValue:@"Ben" forKey:@"first_name"];  
  3. [request setPostValue:@"Copsey" forKey:@"last_name"];  
  4. [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];  
  5. [request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"]; 

如果要發(fā)送自定義數(shù)據(jù):

  1. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  2. [request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];  
  3. // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:  
  4. [request setRequestMethod:@"PUT"]; 

下載文件

通過設(shè)置request的setDownloadDestinationPath,可以設(shè)置下載文件用的下載目標(biāo)目錄。

首先,下載過程文件會保存在temporaryFileDownloadPath目錄下。如果下載完成會做以下事情:

如果數(shù)據(jù)是壓縮的,進(jìn)行解壓,并把文件放在 downloadDestinationPath 目錄中,臨時文件被刪除
如果下載失敗,臨時文件被直接移到 downloadDestinationPath 目錄,并替換同名文件

如果你想獲取下載中的所有數(shù)據(jù),可以實(shí)現(xiàn) delegate 中的 request:didReceiveData:方法。但如果你實(shí)現(xiàn)了這個方法,request 在下載完后,request 并不把文件放在 downloadDestinationPath 中,需要手工處理。

獲取響應(yīng)信息

信息:status , header, responseEncoding

  1. [request responseStatusCode];  
  2. [[request responseHeaders] objectForKey:@"X-Powered-By"];  
  3. [request responseEncoding]; 

獲取請求進(jìn)度

有兩個回調(diào)方法可以獲取請求進(jìn)度:

  1. downloadProgressDelegate,可以獲取下載進(jìn)度   
  2. uploadProgressDelegate,可以獲取上傳進(jìn)度  

cookie的支持

如果 Cookie 存在的話,會把這些信息放在 NSHTTPCookieStorage 容器中共享,并供下次使用。你可以用 [ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有 Cookies。當(dāng)然,你也可以取消默認(rèn)的Cookie策略,而使自定義的Cookie:

  1. //Create a cookie  
  2. NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];  
  3. [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];  
  4. [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];  
  5. [properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];  
  6. [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];  
  7. [properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];  
  8. NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];  
  9.  
  10. //This url will return the value of the 'ASIHTTPRequestTestCookie' cookie  
  11. url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"];  
  12. request = [ASIHTTPRequest requestWithURL:url];  
  13. [request setUseCookiePersistence:NO];  
  14. [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];  
  15. [request startSynchronous];  
  16.  
  17. //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'  
  18. NSLog(@"%@",[request responseString]); 

大文件斷點(diǎn)續(xù)傳0.94 以后支持大文件的斷點(diǎn)下載,只需要設(shè)置

  1. [ request setAllowResumeForFileDownloads:YES ];  
  2. [ request setDownloadDestinationPath:downloadPath ]; 

就可以了。

小結(jié):iPhone應(yīng)用開發(fā)中ASIHTTPRequest詳解的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

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

2011-08-11 13:35:28

iPhoneASIHTTPRequHTTP

2011-08-12 13:35:23

iPhone文件流ASIHTTPRequ

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 11:37:20

iPhone開發(fā)Mask

2011-08-17 15:10:21

iPhone開發(fā)Web視圖

2011-08-22 10:06:38

IOS開發(fā)ASIHTTPRequHTTP 請求

2011-08-02 17:27:06

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

2011-07-27 11:14:37

iPhone UITableVie

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ù)

2013-07-22 13:48:55

iOS開發(fā)ASIHTTPRequ使用Cookie

2011-08-09 14:08:51

iPhoneHTTP請求協(xié)議

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片

2011-08-22 14:12:48

iPhone開發(fā)NSTableView

2011-07-27 10:16:41

iPhone SQLite 數(shù)據(jù)庫

2011-08-12 14:04:53

iPhone動畫
點(diǎn)贊
收藏

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