Discussion on the solution of body loss problem when Nsurlprotocol intercept nsurlsession request

Source: Internet
Author: User
Tags curl aliyun

"IP Direct solution" is mainly to solve DNS pollution, eliminate the DNS resolution time, usually we can use Nsurlprotocol interception nsurlsession request in the project, the following will support the Post request in the face of a challenge, as well as the strategy to explain:

There are several workarounds for missing body issues during post requests:

The scheme is as follows: Swap with nsurlconnection the body into the Header using Httpbodystream to get the body, and assign a value to the body swap with GET request, do not use the Post request.

The following analysis is done for the scheme to swap with nsurlconnection. Nsurlconnection and Nsurlsession will encounter more performance problems, and some of Apple's new features can not be used, will eventually be eliminated, not for consideration. Body put the header method, 2M is no problem, more than 2M will cause the request delay, more than 10M directly request timeout. It is not possible to solve the problem of Body binary data because the header is text data. Swap with a Get request without using a Post request. This is also possible, but after all, there is a limit to the way the request, after all, to solve the problem of the Post request. If modifications are made based on the old project, then the intrusion is too large. This scheme is suitable for new projects.

Another way is the main thing we want to talk about, using Httpbodystream to get the body, and assigned to the body, the specific code as follows, you can solve the problem mentioned above:


The above is the implementation I gave, here is to note that at first someone has done this:

The problem with this implementation is that [stream hasbytesavailable] cannot be used to judge that the [stream hasbytesavailable] will always return yes when processing the picture file, resulting in a loop in the while.

Apple's documentation also made it clear that:

Given the implementation, the following describes the use of the method:

Implement method +canonicalrequestforrequest in the subclass of the nsurlprotocol that is used to intercept the request: and process the Request object:

The following describes the role of the relevant methods:


Translated by:


Simply put: +[nsurlprotocol Caninitwithrequest:] Responsible for filtering which network requests need to be intercepted +[nsurlprotocol Canonicalrequestforrequest:] The nsurlrequest is responsible for re-structuring the network request that needs to be intercepted.

Here's a note: +[nsurlprotocol canonicalrequestforrequest:] The execution condition is +[nsurlprotocol caninitwithrequest:] The return value is yes.

Note When intercepting a nsurlsession request, you need to add the subclass of the Nsurlprotocol that was used to intercept the request to Nsurlsessionconfiguration, using the following:
swap with other lower-level network libraries that provide the SNI field configuration Interface

If using a third-party network library: Curl, there is a-resolve method that can be implemented using the specified IP to access the HTTPS Web site, IOS Integrated Curl Library, reference curl document;

There is also a point to note that it is also supported IPV6 environment, only you need to add--enable-ipv6 on the build.

Curl supports specifying the SNI field, which we need to construct when setting SNI: {HTTPS domain name}:443:{IP address}

Suppose you want to visit. www.example.org, if IP is 127.0.0.1, then this is the way to set the SNI to:

Curl *--resolve ' www.example.org:443:127.0.0.1 ' iOS CURL library

Use Libcurl to resolve, Libcurl/curl at least 7.18.1 (March 30, 2008) compile an SSL/TLS toolkit with SNI support, and CURL has a--resolve method to enable access to HTTPS Web sites using the specified IP.

In the iOS implementation, the code is as follows

wherein the Curlhost shape is as follows:

{HTTPS domain}:443:{ip address}

_hosts_list is a struct type hosts_list that can set the mapping between multiple IPs and host. The Curl_easy_setopt method is passed in to Curlopt_resolve to set the mapping to the HTTPS request.

This allows for the purpose of setting up SNI.

I wrote a demo:cylcurlnetworking here that contains a compiled Libcurl package that supports IPV6, demonstrating how to do similar nsurlsession with curl. Reference Links: Apple-communicating with HTTP Servers Apple-https server Trust evaluation-server Name failures Apple-https Server Trust evaluation-trusting One specific Certificate [Httpdns > Best Practices > HTTPS (including SNI) scenario description of the IP direct connection
HTTPS (including SNI) business scenario "IP Direct Connection" scenario description "(HTTPS://HELP.ALIYUN.COM/DOCUMENT_DETAIL/30143.HTML?SPM=5176.DOC30141.6.591.A8B1D3) Request HTTPS with specified IP in Curl supports SNI and WebView's Alicloud-ios-demo "SNI: Implementing SSL/TLS authentication for multi-domain virtual hosts" Supplemental information

Note that the above discussion does not involve the body loss problem of intercepting nsurlsession requests in Wkwebview.

Several concepts are mentioned in the article:

The domain names mentioned in this article, if no special instructions refer to the FQDN.

Original: https://yq.aliyun.com/articles/543412/?spm=a2c41.11181499.0.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.