在我的 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: This other question, 这里,向我指出了那个解决方案:
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
实现中发送到 http://
和 POST 请求到您的服务器,使用请求查询字符串作为 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
方法,但在 Transfer-Encoding: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!