我正在寻找一种在应用程序级proxyHost/Port设置中使用NSMutableURLRequest的方法,本质上是用proxyHost/proxyPort替换ASIHTTPRequest库。我尝试过修改CFReadStream (来自NSURLRequest HTTPBodyStream),但是它在设置代理设置时使用了SIGSEG。我不想用CFNetworking重写我的应用程序,看起来AFNetwork库也不包含这个特性。
有人用NSMutableURLRequest成功地做到了这一点吗?
发布于 2013-09-30 22:49:17
真正的答案似乎是创建一个自定义NSURLProtocol。应该是一个直接的前向派生,并将适当的proxyHost/proxyPort添加到请求中(以及任何其他值,例如自定义的用户代理字符串)。然后,据推测,所有请求都将通过这个自定义协议(包括UIWebView、requests..direct或派生)进行路由。
有关样本:
http://eng.42go.com/customizing-uiwebview-requests-with-nsurlprotocol/
当我开始操作的时候我会发布更多的信息。
在这个实现中值得注意的事情。
- a) All dependent page resources are loaded automatically by UIWebView (which we knew), and they all go through NSURLProtocol, so it is an excellent place to put in code to modify all requests.
- b) The UIWebView sets the Referer header. On a redirect, the only way to get the UIWebView to update it's Referer from the original URL to the new redirect URL is with the `[[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:tmpHttpResponse];` callback.
- c) when the above redirect callback is received by UIWebView, it生成一个新的NSURLRequest (实质上是您发送回的那个)
)。所以如果你有个楔子喜欢重定向
在内部,您必须取消它试图提出的请求,以支持来自UIWebView的新请求。
didReceiveData将禁用内置的gzip处理。willRedirectToURL禁用大多数内置的重定向处理(即使您按照注释中的建议调用[request redirectToURL:newURL]; )。https://stackoverflow.com/questions/18388633
复制相似问题