Analysis of HTTPS by OKhttp3

Keywords: OkHttp DNS

http://blog.csdn.net/lmj623565791/article/details/48129405 This article of Hongyang God is very clear, but with the revision of OKhttp, some methods no longer exist.
for instance:

mOkHttpClient.setSslSocketFactory(sslContext.getSocketFactory());

There is no such method.
But as long as OKhttp still supports HTTPS parsing, there must be similar methods to use.
Then we look at the source code of OKhttpClient: found this method

public Builder sslSocketFactory(SSLSocketFactory sslSocketFactory) 

We know that OKhttp is usually created through builder during initialization, so that we can set

Builder(OkHttpClient okHttpClient) {
      this.dispatcher = okHttpClient.dispatcher;
      this.proxy = okHttpClient.proxy;
      this.protocols = okHttpClient.protocols;
      this.connectionSpecs = okHttpClient.connectionSpecs;
      this.interceptors.addAll(okHttpClient.interceptors);
      this.networkInterceptors.addAll(okHttpClient.networkInterceptors);
      this.proxySelector = okHttpClient.proxySelector;
      this.cookieJar = okHttpClient.cookieJar;
      this.internalCache = okHttpClient.internalCache;
      this.cache = okHttpClient.cache;
      this.socketFactory = okHttpClient.socketFactory;
      this.sslSocketFactory = okHttpClient.sslSocketFactory;
      this.certificateChainCleaner = okHttpClient.certificateChainCleaner;
      this.hostnameVerifier = okHttpClient.hostnameVerifier;
      this.certificatePinner = okHttpClient.certificatePinner;
      this.proxyAuthenticator = okHttpClient.proxyAuthenticator;
      this.authenticator = okHttpClient.authenticator;
      this.connectionPool = okHttpClient.connectionPool;
      this.dns = okHttpClient.dns;
      this.followSslRedirects = okHttpClient.followSslRedirects;
      this.followRedirects = okHttpClient.followRedirects;
      this.retryOnConnectionFailure = okHttpClient.retryOnConnectionFailure;
      this.connectTimeout = okHttpClient.connectTimeout;
      this.readTimeout = okHttpClient.readTimeout;
      this.writeTimeout = okHttpClient.writeTimeout;
    }

Then we can see this.sslSocketFactory, so we can change the method to:
builder.sslSocketFactory(setCertificates(context.getAssets().open("-12306cn.crt")));
Because the 12306 interface is nowSo I haven't found a website to test yet. You can test and use what you are interested in.

Posted by ErikTheViking on Sat, 02 May 2020 17:33:16 -0700