1

我使用帶有委託的NSURLConnection向URL發送HTTP GET請求。該請求被重定向到HTTP 302,並執行新的請求並檢索數據。使用NSURLConnection獲取302 HTTP重定向的響應主體

問題是我不希望重定向的HTTP請求的主體,但原始重定向響應的內容。

我已經實現了- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response這是用redirectRepsonse調用,但connection:didReceiveData直到新的重定向請求返回時才被調用。

迄今爲止,除了使用基於CFNetwork的方法之外,我還沒有找到解決方案。

更新: 我已經創建與您一起玩這個問題的示例項目:https://github.com/snod/302RedirectTest

任何想法?

+0

你試過實現NSURLConnectionDataDelegate的方法呢? – orkoden 2014-09-05 10:11:41

+0

當然,問題中提到的那兩種方法來自該協議。 – snod 2014-09-05 11:25:39

+0

你有沒有得到這個工作?我有同樣的問題。 – Locksleyu 2017-09-07 23:20:22

回答

2

-connection:willSendRequest:redirectResponse:docs

To receive the body of the redirect response itself, return nil to cancel the redirect. The connection continues to process, eventually sending your delegate a connectionDidFinishLoading or connection:didFailLoadingWithError: message, as appropriate.

+0

我已經試過,沒有成功。當重定向響應發送給委託後你返回nil時,連接將會在沒有調用URL連接委託方法的情況下丟棄。 – snod 2014-09-05 13:57:52

+0

如果您返回原始請求,會發生什麼情況? (實際上,如果你接到一個規範化原始請求的呼叫,你可能會想要保留這個請求並將它返回給重定向請求。) – 2014-09-06 05:04:42

+0

也不行,每次都得到相同的重定向響應,直到有很多他們和服務器保釋出來。 – snod 2014-09-06 10:04:17