gpt4 book ai didi

javax.net.ssl.SSLException : SSL handshake aborted Connection reset by peer while calling webservice Android

转载 作者:IT王子 更新时间:2023-10-28 23:36:00 31 4
gpt4 key购买 nike

我正在调用 https webservice,它以前工作正常,但现在当我尝试调用它时,它会给我以下错误。

日志错误:

12-23 06:28:11.969: W/System.err(3014): javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x1cc160: I/O error during system call, Connection reset by peer
12-23 06:28:11.979: W/System.err(3014): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
12-23 06:28:11.979: W/System.err(3014): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:474)
12-23 06:28:11.979: W/System.err(3014): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:750)
12-23 06:28:11.979: W/System.err(3014): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:692)
12-23 06:28:11.979: W/System.err(3014): at crittercism.android.aa.getInputStream(Unknown Source)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:93)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
12-23 06:28:11.979: W/System.err(3014): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:171)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-23 06:28:11.989: W/System.err(3014): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

我正在使用以下代码调用 https 网络服务。

public static void trustAllHosts() {

X509TrustManager easyTrustManager = new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
// Oh, I am easy!
}

public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
// Oh, I am easy!
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}

};

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { easyTrustManager };

// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());

} catch (Exception e) {
e.printStackTrace();
}
}

public static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);

SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);

return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}

MySSLSocketFactory.java

public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");

public MySSLSocketFactory(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);

TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};

sslContext.init(null, new TrustManager[] { tm }, null);
}

@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port,
autoClose);
}

@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}

以前可以正常使用,现在不行了。服务器中没有任何更改。

我已经推荐了

  1. Android-Query - Random SSLExceptions
  2. Intermittent Connection Reset by Peer errors in Android connecting to .NET REST endpoint
  3. Android HTTPS exception Connection reset by peer
  4. Why is HttpUrlConnection throwing an SSLException while on a mobile data connection?

我已经在 wifi 和移动数据中对其进行了测试。应用程序不适用于两者。

如果有人在此之前遇到过这个问题,请帮助我解决它。

最佳答案

我也遇到了同样的异常。我发现这是由于服务器不支持 TLS 1.0 协议(protocol)

我观察到 Android 设备,http 连接到不支持 TLS 1.0 的服务器失败。我搜索了有关该错误的所有地方,但没有找到与此问题相关的任何内容。当TLS 1.0 协议(protocol) 支持添加到服务器后,问题就解决了。您可以使用https://www.ssllabs.com/ssltest 检查您的服务器/主机名协议(protocol)支持。 .

关于javax.net.ssl.SSLException : SSL handshake aborted Connection reset by peer while calling webservice Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741405/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com