1

我知道已经有类似的问题,但没有一个给我解决这个问题的方法。

我正在尝试连接到一些 XML API,如果我在 Postman 中或仅通过浏览器执行此操作,一切正常,并且正在返回 XML。

但是,如果我发送GET到这个 URL,我会得到:

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 中无效?我的第二个问题 - 如何解决这个问题?是安卓的问题还是服务器的问题?我已经尝试了这两种解决方案,但它们都不起作用:

    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;
}
4

0 回答 0