A brief review of the Okhttp execution process

Basic process:

EventListener: This is basically a listener that listens for the entire OKHTTP connection to be established, released, etc. (TCP and HTTP can listen for this) look at the newRealCall parameter

OkHttpClient: This is a set of HTTP properties that will be covered later.

Request originalRequest: This is the Request we passed in, but why is it called originalRequest? And it’s not that hard to do primarily because the request that we send in is not necessarily the request that we end up sending out because it’s going to go through the chain of responsibility, and we’ll talk about that later

Let’s take a look at the Dispatcher

MaxRequests: The maximum number of requests that can be run simultaneously, i.e., an OKHTTP client can send 64 requests simultaneously

MaxRequestsPerHost: We can have a maximum of 5 requests per domain name at one time.

The actual meaning of this parameter is that okHTTP can initiate up to five TCP connections per domain name.

ReadyAsyncCalls: This is a request that will be sent but has not been sent yet

RunningAsyncCalls: Requests in progress

The Dispatcher is basically a thread scheduler

Look at the execution;

The real execution is AsyncCall runnable

Here you can see that the AsyncCall execute method is actually executed

From the analysis, we can get two conclusions:

The entire request issued including the results returned are in this getResponseWithInterceptorChain method

The other thing is that you can see that the okHTTP result callback is actually in the child thread

OkhttpClient heavy and difficult parameter parsing

Here I will pick some difficult parameters to parse, because most of the other articles cover these details very little. I’m not going to talk about some of the things that everybody says.

Let’s focus on these three parameters

HostnameVerifier: This looks like a domain name validator, but is essentially a certificate validator.

Is to do certificate verification, you can see his verification process

Notice why we’re taking the head of an array, right? Take the first element, right? In fact, during the establishment of a TLS connection, the certificate transmitted by the server to the client contains not only the server’s own certificate, but also the certificate of other signing institutions

Open a browser to see, we see baidu’s certificate, you see it is a chain structure, the bottom is baidu’s certificate, who gave him the issue? Signed by a secondary signature authority

Let’s add a log to see if this is true:

Then continue to watch his verification process:

I’m just checking what’s in the certificate

This is actually to verify that the domain name information in the website certificate and the destination address is a match. For example, the domain name of Baidu THAT I visited, if you give me a certificate, it is the domain name of Google, it must fail verification.

Anyone who has done HTTPS certificate applications knows that you submit a domain name when you apply for a certificate, and you have to prove that the owner of the domain name is you

CertificateChainCleaner: As mentioned above, when establishing TLS connection, the certificate returned by the server to the client is one ring after another, because the certificate of the server will be signed by the organization. In this case, the certificate wrapped outside the signature structure is stripped away, leaving only the final certificate of the server

CertificatePinner: Certificate holder

This is used to fix the certificate. For example, for my project, I don’t want to apply for the signature of CA or other institutions. I don’t need to register with them, I just need to get a private HTTPS certificate. We can then build the value of this public key into a Certificate inside. This also allows TLS authentication.

Or I just want to build in certificates, for anti-capture and so forth and you can build in certificates with this.

But the downside of this is that once you build it into your app, if your server says I need to update the certificate, it’s a problem. Because the public keys don’t match. All online user access fails.

DNS: searches for DNS

The only thing to note here is that usually a domain name is configured with multiple IP addresses. What okHTTP does well here is that when a TCP connection with one IP fails, it automatically switches to another IP to try to continue

redirect: Redirection related

If you want to redirect followSsl to an HTTP address with HTTPS, you can configure this property

GetResponseWithInterceptorChain,

Just understand the concept of a chain of responsibility

The concept of the OKHTTP responsibility chain is simple;

There are n nodes in a chain. The execution sequence is as follows:

The first node performs its own operations —— the second node performs its own operations —– the third node —- the NTH node performs its own operations

And then the NTH node realizes that there’s no node behind it, so the NTH node will continue to do its own post-operation after it’s done

It can return the result of the execution to the NTH node, and then the NTH node takes the result of the NTH node and performs its own post-operation

This is done until the second node returns the result of its post-operation to the first node, and then the first node completes all operations.

It’s much easier to look at the code once you understand the logic. And keep that in mind when you look at the code

Any place where the proceed method is called means that the process of calling the chain at that point goes to the next chain. Proceed before proceed and proceed after.

RetryAndFollowUpInterceptor: just as its name implies Here is mainly to request connection failure Heavy trial.

This is a simple for loop that throws an exception up if the connection fails, and then continues in the exception to retry the connection

BridgeInterceptorThe gzip attribute will be added automatically to the HTTP header request

Continue to look at:

The same is true here, where gzip is compressed when the request is returned

CacheInterceptor Cache-specific

The only thing to notice here is that if you trigger the cache-dependent mechanism, you end the chain of responsibility prematurely here and all the subsequent chains don’t go any further.

ConnectInterceptor this is the core of okHTTP

It is the core of the whole TCP-HTTP codec. In fact, TCP and TLS connections are established in this process

Once you get httpCodec here, you can move on to the next process

CallServerInterceptor

This is where you write output and read data into the socket. And that’s where the chain ends

It’s not hard to look through okHTTP’s chain of responsibilities. It’s just a few details that need to be thought through and read slowly and thoughtfully.

The other is

Many people know the difference between interceptors and networkInterceptors, but the main reason is that the Bridge interceptor can gzip the request The network interceptor is useless to the average developer in most scenarios. You can think of this as a hook for handling bytes data directly for a particular scenario