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

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

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

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

下面就舉例說(shuō)明它的 API 用法。

發(fā)起一個(gè)同步請(qǐng)求

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

  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 的一個(gè)實(shí)例

startSynchronous 方法啟動(dòng)同步訪(fǎng)問(wèn)

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

responseString,為請(qǐng)求的返回 NSString 信息

創(chuàng)建一個(gè)異步請(qǐng)求

異步請(qǐng)求的好處是不阻塞當(dāng)前線(xiàn)程,但相對(duì)于同步請(qǐng)求略為復(fù)雜,至少要添加兩個(gè)回調(diào)方法來(lái)獲取異步事件。下面異步請(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];  

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

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

隊(duì)列請(qǐng)求

提供了一個(gè)對(duì)異步請(qǐng)求更加精準(zhǔn)豐富的控制。如:可以設(shè)置在隊(duì)列中同步請(qǐng)求的連接數(shù)。往隊(duì)列里添加的請(qǐng)求實(shí)例數(shù)大于 maxConcurrentOperationCount 時(shí),請(qǐng)求實(shí)例將被置為等待,直到前面至少有一個(gè)請(qǐng)求完成并出列才被放到隊(duì)列里執(zhí)行。這也適用于當(dāng)我們有多個(gè)請(qǐng)求需求按順序執(zhí)行的時(shí)候(可能是業(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,這個(gè) Cocoa 架構(gòu)的執(zhí)行任務(wù)(NSOperation)的任務(wù)隊(duì)列。我們通過(guò) ASIHTTPRequest.h 的源碼可以看到,此類(lèi)本身就是一個(gè) NSOperation 的子類(lèi)。也就是說(shuō)它可以直接被放到"任務(wù)隊(duì)列"中并被執(zhí)行。上面的代碼除了隊(duì)列的創(chuàng)建與添加操作外,其它代碼與上一例一樣。

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

可以設(shè)置一個(gè)上下文(userInfo)到 request 對(duì)象中,當(dāng)請(qǐng)求響應(yīng)完后可以通過(guò)訪(fǎng)問(wèn) request 對(duì)象的 userInfo 獲取里面的信息

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

子類(lèi)化 ASIHTTPRequest,重寫(xiě) requestFinished: 與 failWithProblem: 方法

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

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

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

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

取消異步請(qǐng)求

首先,同步請(qǐng)求是不能取消的。

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

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

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

  1. [ queue setShouldCancelAllRequestsOnFailure:NO ];  

如果想明確取消所有請(qǐng)求:[ queue cancelAllOperations ];

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

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

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

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

ASIFormDataRequest ,模擬 Form 表單提交,其提交格式與 Header 會(huì)自動(dòng)識(shí)別。

沒(méi)有文件: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"]; 

下載文件

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

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

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

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

獲取響應(yīng)信息

信息:status , header, responseEncoding

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

獲取請(qǐng)求進(jìn)度

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

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

cookie的支持

如果 Cookie 存在的話(huà),會(huì)把這些信息放在 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)用開(kāi)發(fā)中ASIHTTPRequest詳解的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(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開(kāi)發(fā)Mask

2011-08-17 15:10:21

iPhone開(kāi)發(fā)Web視圖

2011-08-22 10:06:38

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

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

2011-08-17 15:19:38

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

2013-07-22 13:48:55

iOS開(kāi)發(fā)ASIHTTPRequ使用Cookie

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-09 14:08:51

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

2011-08-15 15:44:46

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

2011-08-18 16:24:44

iPhone開(kāi)發(fā)圖片

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-27 10:16:41

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

2011-08-22 14:12:48

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

2011-08-12 14:04:53

iPhone動(dòng)畫(huà)
點(diǎn)贊
收藏

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