android okhttp 忽略证书
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
OkHttpClient client = getUnsafeOkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS) //设置连接超时时间
.readTimeout(60, TimeUnit.SECONDS) //设置读取超时时间
.writeTimeout(60, TimeUnit.SECONDS).build(); //设置写入超时时间;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
public class ClientT
{
public static OkHttpClient getUnsafeOkHttpClient()
{
try
{
final TrustManager[] trustAllCerts = new TrustManager[]
{
new X509TrustManager()
{
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
{}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
{}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return new java.security.cert.X509Certificate[]
{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
return true;
}
});
return builder.build();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}