我知道已经有类似的问题了,但是没有一个能给我解决这个问题的方法。
我正在尝试连接到某些XML API,如果我在Postman中执行此操作,或者只是通过浏览器,一切正常并且正在返回XML。
但是,如果我将GET
发送到此网址,我会收到:
W/System.err: javax.net.ssl.SSLHandshakeException: Connection closed by peer
W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
W/System.err: at com.squareup.okhttp.Connection.connectTls(Connection.java:239)
W/System.err: at com.squareup.okhttp.Connection.connectSocket(Connection.java:201)
W/System.err: at com.squareup.okhttp.Connection.connect(Connection.java:172)
W/System.err: at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:358)
W/System.err: at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:117)
W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
W/System.err: at com.squareup.okhttp.Call.getResponse(Call.java:276)
W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:234)
W/System.err: at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:136)
W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:223)
W/System.err: at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:196)
W/System.err: at com.squareup.okhttp.Call.access$100(Call.java:34)
W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Suppressed: javax.net.ssl.SSLHandshakeException: Connection closed by peer
W/System.err: ... 20 more
我的第一个问题 - 为什么它在浏览器中有效但在Android中无效? 我的第二个问题 - 如何解决这个问题? Android或服务器有问题吗?我尝试了这两种解决方案,但没有一种解决方案:
private static OkHttpClient createClient() {
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
client.interceptors().add(interceptor);
ConnectionSpec spec = 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,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
client.setConnectionSpecs(Collections.singletonList(spec));
return client;
}
和
private static OkHttpClient createClient() {
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
client.interceptors().add(interceptor);
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
client.setSslSocketFactory(sslSocketFactory);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
return client;
}