• <tfoot id='qOXPn'></tfoot>
  • <legend id='qOXPn'><style id='qOXPn'><dir id='qOXPn'><q id='qOXPn'></q></dir></style></legend>
    1. <small id='qOXPn'></small><noframes id='qOXPn'>

        • <bdo id='qOXPn'></bdo><ul id='qOXPn'></ul>

        <i id='qOXPn'><tr id='qOXPn'><dt id='qOXPn'><q id='qOXPn'><span id='qOXPn'><b id='qOXPn'><form id='qOXPn'><ins id='qOXPn'></ins><ul id='qOXPn'></ul><sub id='qOXPn'></sub></form><legend id='qOXPn'></legend><bdo id='qOXPn'><pre id='qOXPn'><center id='qOXPn'></center></pre></bdo></b><th id='qOXPn'></th></span></q></dt></tr></i><div id='qOXPn'><tfoot id='qOXPn'></tfoot><dl id='qOXPn'><fieldset id='qOXPn'></fieldset></dl></div>

        使用带有 UIWebView 和 POST 请求的自定义 NSURLProtocol

        时间:2023-11-09

          <bdo id='UvKzS'></bdo><ul id='UvKzS'></ul>
            <tbody id='UvKzS'></tbody>
        • <i id='UvKzS'><tr id='UvKzS'><dt id='UvKzS'><q id='UvKzS'><span id='UvKzS'><b id='UvKzS'><form id='UvKzS'><ins id='UvKzS'></ins><ul id='UvKzS'></ul><sub id='UvKzS'></sub></form><legend id='UvKzS'></legend><bdo id='UvKzS'><pre id='UvKzS'><center id='UvKzS'></center></pre></bdo></b><th id='UvKzS'></th></span></q></dt></tr></i><div id='UvKzS'><tfoot id='UvKzS'></tfoot><dl id='UvKzS'><fieldset id='UvKzS'></fieldset></dl></div>
          • <tfoot id='UvKzS'></tfoot>

            <small id='UvKzS'></small><noframes id='UvKzS'>

              <legend id='UvKzS'><style id='UvKzS'><dir id='UvKzS'><q id='UvKzS'></q></dir></style></legend>

                  本文介绍了使用带有 UIWebView 和 POST 请求的自定义 NSURLProtocol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:两个单独的 cookie 存储(UIWebView 或 WKWebView) 下一篇:从 UIWebView 检测 window.open()

                  相关文章

                  1. <i id='gvQqJ'><tr id='gvQqJ'><dt id='gvQqJ'><q id='gvQqJ'><span id='gvQqJ'><b id='gvQqJ'><form id='gvQqJ'><ins id='gvQqJ'></ins><ul id='gvQqJ'></ul><sub id='gvQqJ'></sub></form><legend id='gvQqJ'></legend><bdo id='gvQqJ'><pre id='gvQqJ'><center id='gvQqJ'></center></pre></bdo></b><th id='gvQqJ'></th></span></q></dt></tr></i><div id='gvQqJ'><tfoot id='gvQqJ'></tfoot><dl id='gvQqJ'><fieldset id='gvQqJ'></fieldset></dl></div>
                  2. <small id='gvQqJ'></small><noframes id='gvQqJ'>

                    <tfoot id='gvQqJ'></tfoot>

                      <legend id='gvQqJ'><style id='gvQqJ'><dir id='gvQqJ'><q id='gvQqJ'></q></dir></style></legend>

                      • <bdo id='gvQqJ'></bdo><ul id='gvQqJ'></ul>