使用带有UIWebView和POST请求的自定义NSURLProtocol [英] Using a custom NSURLProtocol with UIWebView and POST requests
问题描述
在我的iOS应用中,我使用的是UIWebView和自定义协议(使用我自己的NSURLProtocol实现)。我一直非常小心确保每当我加载一个url时,我将这样的内容加载到我的UIWebView中:
In my iOS app, I'm using a UIWebView and a custom protocol (with my own NSURLProtocol implementation). I've been fairly careful about making sure that whenever I load a url, I load something like this into my UIWebView:
myprotocol:// myserver / mypath
myprotocol://myserver/mypath
在我的NSURLProtocol实现中,我获取NSURLRequest的可变副本,将URL转换为http:并将其发送到我的服务器。
and in my NSURLProtocol implementation, I take a mutable copy of the NSURLRequest, convert the URL to http: and send that to my server.
一切都适用于HTTP GET请求。我遇到的问题是POST请求。如果请求使用我的自定义协议,似乎UIWebView没有正确编码HTTPBody中的表单数据。
Everything works for HTTP GET requests. The problem I encounter is with POST requests. It seems like the UIWebView doesn't properly encode the form data in the HTTPBody if the request uses my custom protocol.
一个解决方法,因为我使用HTTPS对于我的服务器请求,是我注册我的协议处理程序来拦截http:而不是myprotocol:我可以将所有调用转换为https:另一个问题,这里,指向我解决方案:
One work-around, since I'm using HTTPS for my server requests, is that I register my protocol handler to intercept http: instead of myprotocol: and I can convert all calls to https: This other question, here, pointed me toward that solution:
但是我我想知道是否有任何替代和/或更好的方法来实现我想要的东西。
But I'm wondering if there's any alternative and/or better way of accomplishing what I want.
推荐答案
而不是尝试使用POST请求,一个解决方法是继续使用 myprotocol://
URL的GET请求,但在 NSURLProtocol
实现中转换它们使用请求查询字符串作为POST的主体,向服务器发送 http://
和 POST请求。
Instead of trying to use POST requests, one work around is to continue using GET requests for myprotocol://
URLs, but transform them in your NSURLProtocol
implementation to an http://
and POST request to your server using the request query string as the body of the POST.
使用GET请求发送大量数据的担忧是在请求链的某个地方,请求行可能会被截断。然而,这似乎不是一个问题,使用本地实现的协议。
The worry with using GET requests to send large amounts of data is that somewhere along the request chain, the request line might get truncated. This appears to not be a problem, however, with locally-implemented protocols.
我写了一个简短的Cordova测试应用程序进行实验,我发现我能够通过超过1 MiB的数据,没有麻烦的HTTP请求回显服务 http://http-echo.jgate.de/
I wrote a short Cordova test app to experiment and I found that I was able to send through a little over 1 MiB of data without trouble to the HTTP request echoing service http://http-echo.jgate.de/
这是我的 startLoading
实施:
- (void)startLoading {
NSURL *url = [[self request] URL];
NSString *query = [url query];
// Create a copy of `url` without the query string.
url = [[[NSURL alloc] initWithScheme:@"http" host:@"http-echo.jgate.de" path:[url path]] autorelease];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:url];
[newRequest setHTTPMethod:@"POST"];
[newRequest setAllHTTPHeaderFields:[[self request] allHTTPHeaderFields]];
[newRequest addValue:@"close" forHTTPHeaderField:@"Connection"];
[newRequest addValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[newRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
urlConnection = [[NSURLConnection alloc] initWithRequest:newRequest delegate:self];
if (urlConnection) {
receivedData = [[NSMutableData data] retain];
}
}
然后我实现了 NSURLConnection
转发到相应的 NSURLProtocolClient
方法的协议方法,但是在转发编码的情况下构建响应数据: chunked
(就像 http://http-echo.jgate.de/)。
I then implemented the NSURLConnection
protocol methods to forward to the appropriate NSURLProtocolClient
method, but building up the response data in the case of Transfer-Encoding:chunked
(as is the case for responses from http://http-echo.jgate.de/).
这篇关于使用带有UIWebView和POST请求的自定义NSURLProtocol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!