Skip to content

Commit

Permalink
Merge pull request #55 from yanghaiji/demo
Browse files Browse the repository at this point in the history
add okhttp  ➕ 🆕
  • Loading branch information
yanghaiji authored Dec 22, 2021
2 parents cf30a14 + cf1c635 commit 696d848
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -49,6 +50,7 @@ private OkHttpClientApp() {
.readTimeout(20, TimeUnit.SECONDS)
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
.hostnameVerifier((hostName, session) -> true)
.connectionSpecs(Collections.singletonList(createTls()))
.retryOnConnectionFailure(true)
.build();
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
Expand All @@ -59,7 +61,6 @@ private OkHttpClientApp() {

/**
* 用于异步请求时,控制访问线程数,返回结果
*
*/
private static Semaphore getSemaphoreInstance() {
//只能1个线程同时访问
Expand Down Expand Up @@ -263,7 +264,6 @@ private void setHeader(Request.Builder request) {

/**
* 生成安全套接字工厂,用于https请求的证书跳过
*
*/
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
SSLSocketFactory ssfFactory = null;
Expand Down Expand Up @@ -296,6 +296,21 @@ public X509Certificate[] getAcceptedIssuers() {
};
}

/**
* 指定 tls 版本
* @return
*/
private ConnectionSpec createTls() {
return new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();
}


/**
* 自定义一个接口回调
*/
Expand Down

0 comments on commit 696d848

Please sign in to comment.