这是广泛用作解决方法的不安全的OkHttpClient。
不要在生产中使用它,仅用于开发目的。
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
public class Http {
private final static String SSL = "SSL";
private static OkHttpClient InsecureHttpClient;
public static OkHttpClient client () {
if (InsecureHttpClient == null) {
try {
InsecureHttpClient = insecureOkHttpClient ();
} catch (Exception e) {
e.printStackTrace ();
}
}
return InsecureHttpClient;
}
private static OkHttpClient insecureOkHttpClient () throws Exception {
TrustManager [] trustAllCerts = new TrustManager [] { trustManager () };
SSLContext sslContext = SSLContext.getInstance (SSL);
sslContext.init (null, trustAllCerts, new SecureRandom ());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory ();
OkHttpClient.Builder builder = new OkHttpClient.Builder ();
builder.sslSocketFactory (sslSocketFactory, (X509TrustManager)trustAllCerts [0]);
builder.hostnameVerifier (hostnameVerifier ());
return builder.build ();
}
private static TrustManager trustManager () {
return new X509TrustManager () {
@Override
public void checkClientTrusted (X509Certificate [] chain, String authType) throws CertificateException { }
@Override
public void checkServerTrusted (X509Certificate[] chain, String authType) throws CertificateException { }
@Override
public X509Certificate [] getAcceptedIssuers () {
return new X509Certificate [] { };
}
};
}
private static HostnameVerifier hostnameVerifier () {
return new HostnameVerifier () {
@Override
public boolean verify (String hostname, SSLSession session) {
return true;
}
};
}
}
然后,您显然可以像上述测试代码一样使用上述客户端:(顺便说一下,它可以与您的网址一起使用)
final Request request = new Request.Builder ()
.url ("https://bits-bosm.org/2017/registrations/signup/")
.get ()
.addHeader ("Accept", "text/html")
.build ();
final OkHttpClient httpClient = Http.client ();
new Thread (new Runnable () {
@Override
public void run () {
try {
Response response = httpClient.newCall (request).execute ();
Logger.error (MainActivity.class.getSimpleName () + " --> Http Response", response.body ().string ());
} catch (IOException e) {
e.printStackTrace ();
}
}
}).start ();