31 lines
1.0 KiB
Java
31 lines
1.0 KiB
Java
|
|
package com.migu.libnetwork;
|
||
|
|
|
||
|
|
import com.migu.base.SSLSocketFactoryCompat;
|
||
|
|
import com.migu.base.TrustAllHostnameVerifier;
|
||
|
|
|
||
|
|
import java.util.concurrent.TimeUnit;
|
||
|
|
|
||
|
|
import okhttp3.OkHttpClient;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 处理证书不受信任的问题
|
||
|
|
*/
|
||
|
|
public class OkHttpSsl {
|
||
|
|
//okHttp3添加信任所有证书
|
||
|
|
public static OkHttpClient getUnsafeOkHttpClient() {
|
||
|
|
try {
|
||
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||
|
|
builder.sslSocketFactory(new SSLSocketFactoryCompat(new HttpsTrustManager())
|
||
|
|
, new HttpsTrustManager());
|
||
|
|
builder.addInterceptor(new HttpRequestInterceptor());
|
||
|
|
builder.hostnameVerifier(new TrustAllHostnameVerifier());//忽略ssl
|
||
|
|
return builder.connectTimeout(10, TimeUnit.SECONDS)
|
||
|
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||
|
|
.readTimeout(10, TimeUnit.SECONDS)
|
||
|
|
.build();
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new RuntimeException(e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|