ASIHTTPRequest處理收到的伺服器響應資料

來源:互聯網
上載者:User
擷取HTTP狀態代碼

ASIHTTPRequest並不對HTTP狀態代碼做任何處理(除了重新導向和授權狀態代碼,下面會介紹到),所以你必須自己檢查狀態值並正確處理。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request startSynchronous];int statusCode = [request responseStatusCode];NSString *statusMessage = [request responseStatusMessage];
 讀取回應標頭
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request startSynchronous];NSString *poweredBy = [[request responseHeaders] objectForKey:@"X-Powered-By"];NSString *contentType = [[request responseHeaders] objectForKey:@"Content-Type"];

 

處理文本編碼

ASIHTTPRequest會試圖讀取返回資料的編碼資訊(Content-Type頭資訊)。如果它發現了編碼資訊,它會將編碼資訊設定為合適的 NSStringEncoding.如果它沒有找到編碼資訊,它會將編碼設定為預設編碼(NSISOLatin1StringEncoding)。

當你調用[request responseString],ASIHTTPRequest會嘗試以responseEncoding將返回的Data轉換為NSString。

處理重新導向

當遇到以下HTTP狀態代碼之一時,ASIHTTPRequest會自動重新導向到新的URL:

  • 301 Moved Permanently
  • 302 Found
  • 303 See Other

當發生重新導向時,響應資料的值(responseHeaders,responseCookies,responseData,responseString等等)將會映射為最終地址的相應返回資料。

當URL發生迴圈重新導向時,設定在這個URL上的cookie將被儲存到全域域中,並在適當的時候隨重新導向的請求發送到伺服器。

Cookies set on any of the urls encountered during a redirection cycle will be stored in the global cookie store, and will be represented to the server on the redirected request when appropriate.

你可以關閉自動重新導向:將shouldRedirect設定為NO。

預設情況下,自動重新導向會使用GET請求(請求體為空白)。這種行為符合大多數瀏覽器的行為,但是HTTP spec規定301和302重新導向必須使用原有方法。

要對301、302重新導向使用原方法(包含請求體),在發起請求之前,設定shouldUseRFC2616RedirectBehaviour 為YES。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.