javax.net.ssl.SSLHandshakeException:com.android.org.conscrypt.NativeCrypto.SSL_do_handshake 处的对等方关闭了连接(本机方法)
javax.net.ssl.SSLHandshakeException: Connection closed by peer at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
在 api 24 级之前,我的代码工作正常,但它在 api 24 级(7.0 Nougat)上给我错误。我不知道我的代码出了什么问题。
第一种方法在这里:
<?xml version="1.0" encoding="utf-8"?> <network-security-config>
<base-config> <trust-anchors> <certificates src="system"/>
<certificates src="user"/> </trust-anchors> </base-config>
<domain-config> <domain includeSubdomains="true">xyz.com</domain>
<trust-anchors> <certificates src="@raw/my_ca"/> </trust-anchors>
</domain-config> </network-security-config>
内部清单文件:
android:network Security Config = "@xml/network_security_config"
我在 res/xml/network_security_config
中包含 network_security_config
并且 ca
证书在 res/raw/my_ca.pem
中
第二种方法在这里:
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException; import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class ExSSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public ExSSLSocketFactory(KeyStore truststore) throws
NoSuchAlgorithmException, KeyManagementException,KeyStoreException,
UnrecoverableKeyException {
super(truststore);
TrustManager x509TrustManager = 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[] {
x509TrustManager }, null);
} public ExSSLSocketFactory(SSLContext
context) throws KeyManagementException, NoSuchAlgorithmException,
KeyStoreException, UnrecoverableKeyException {
super(null);
sslContext = context;
} @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();
}
public static HttpClient getHttpsClient(HttpClient client) {
try{
X509TrustManager x509TrustManager = 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 null;
}
};
SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new
TrustManager[]{x509TrustManager}, null); SSLSocketFactory
sslSocketFactory = new ExSSLSocketFactory(sslContext);
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager clientConnectionManager =
client.getConnectionManager(); SchemeRegistry schemeRegistry =
clientConnectionManager.getSchemeRegistry();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
return new DefaultHttpClient(clientConnectionManager,
client.getParams());
} catch (Exception ex)
{ return null;
} } }
建立 http 连接时:
public String CallWebService(String url, String soapAction, String envelope){
final HttpClient httpClient = ExSSLSocketFactory.getHttpsClient(new DefaultHttpClient());
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 150000);
HttpConnectionParams.setSoTimeout(params, 150000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
HttpPost httppost = new HttpPost(url);
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString = "";
try
{
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
ResponseHandler<String> rh = new ResponseHandler<String>()
{
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException
{
HttpEntity entity = response.getEntity();
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString = httpClient.execute(httppost, rh);
Log.d("Response is here....", "responseString : " + responseString);
}
catch (Exception e)
{
e.printStackTrace();
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
仍然出现错误:
javax.net.ssl.SSLHandshakeException: Connection closed by peer 01-24
10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at
com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357)
01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.Connection.connectTls(Connection.java:235) 01-24
10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at
com.android.okhttp.Connection.connectSocket(Connection.java:199) 01-24
10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at
com.android.okhttp.Connection.connect(Connection.java:172) 01-24
10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at
com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.OkHttpClient.connectAndSetOwner(OkHttpClient.java:130)
01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at
com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at
org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at srd.gshelp.GSSoapConWSDL.javaHit(GSSoapConWSDL.java:180) 01-24
10:28:03.184 32251-474/com.neosoft.meconnect W/System.err: at
srd.gshelp.GSSoapConWSDL.access[=20=](GSSoapConWSDL.java:144) 01-24
10:28:03.185 32251-474/com.neosoft.meconnect W/System.err: at
srd.gshelp.GSSoapConWSDL$TaskAsync.doInBackground(GSSoapConWSDL.java:215)
01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err:
at
srd.gshelp.GSSoapConWSDL$TaskAsync.doInBackground(GSSoapConWSDL.java:1)
01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err:
at android.os.AsyncTask.call(AsyncTask.java:304) 01-24 10:28:03.185
32251-474/com.neosoft.meconnect W/System.err: at
java.util.concurrent.FutureTask.run(FutureTask.java:237) 01-24
10:28:03.186 32251-474/com.neosoft.meconnect W/System.err: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
01-24 10:28:03.186 32251-474/com.neosoft.meconnect W/System.err:
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
01-24 10:28:03.186 32251-474/com.neosoft.meconnect W/System.err:
at java.lang.Thread.run(Thread.java:761) 01-24 10:28:03.187
32251-474/com.neosoft.meconnect W/System.err: Suppressed:
javax.net.ssl.SSLHandshakeException: Connection closed by peer
请帮忙。
提前致谢。
你用过Okhttp库吗?
这是一个非常好的网络调用库。您也可以处理此异常。
我遇到了类似的问题,我已经解决了这个问题:
public static OkHttpClient getHttpClientForFile() {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_0)
.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_DHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA)
.build();
return new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.readTimeout(3, TimeUnit.MINUTES)
.connectionSpecs(Collections.singletonList(spec))
.protocols(Arrays.asList(Protocol.HTTP_1_1))
.build();
}
我不知道它是否好用,但它对我有用。
您使用的 class SSLSocketFactory
可能会在 Play 商店发布应用程序后产生错误,或者 Play 商店可能会警告您更改代码。
您可以从 https://github.com/square/okhttp 找到 Okhttp
库。
确保您已从服务器端设置 TLS 启用。
我遇到了同样的问题,同样的 javax.net.ssl.SSLHandshakeException
在 API 调用时抛出。
但就我而言,以下是问题
我的 device
是 connected
到 wifi
但 wifi 路由器 not
有 internet
连接然后 exception
是抛出
在 api 24 级之前,我的代码工作正常,但它在 api 24 级(7.0 Nougat)上给我错误。我不知道我的代码出了什么问题。
第一种方法在这里:
<?xml version="1.0" encoding="utf-8"?> <network-security-config>
<base-config> <trust-anchors> <certificates src="system"/>
<certificates src="user"/> </trust-anchors> </base-config>
<domain-config> <domain includeSubdomains="true">xyz.com</domain>
<trust-anchors> <certificates src="@raw/my_ca"/> </trust-anchors>
</domain-config> </network-security-config>
内部清单文件:
android:network Security Config = "@xml/network_security_config"
我在 res/xml/network_security_config
中包含 network_security_config
并且 ca
证书在 res/raw/my_ca.pem
第二种方法在这里:
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException; import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class ExSSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public ExSSLSocketFactory(KeyStore truststore) throws
NoSuchAlgorithmException, KeyManagementException,KeyStoreException,
UnrecoverableKeyException {
super(truststore);
TrustManager x509TrustManager = 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[] {
x509TrustManager }, null);
} public ExSSLSocketFactory(SSLContext
context) throws KeyManagementException, NoSuchAlgorithmException,
KeyStoreException, UnrecoverableKeyException {
super(null);
sslContext = context;
} @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();
}
public static HttpClient getHttpsClient(HttpClient client) {
try{
X509TrustManager x509TrustManager = 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 null;
}
};
SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new
TrustManager[]{x509TrustManager}, null); SSLSocketFactory
sslSocketFactory = new ExSSLSocketFactory(sslContext);
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager clientConnectionManager =
client.getConnectionManager(); SchemeRegistry schemeRegistry =
clientConnectionManager.getSchemeRegistry();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
return new DefaultHttpClient(clientConnectionManager,
client.getParams());
} catch (Exception ex)
{ return null;
} } }
建立 http 连接时:
public String CallWebService(String url, String soapAction, String envelope){
final HttpClient httpClient = ExSSLSocketFactory.getHttpsClient(new DefaultHttpClient());
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 150000);
HttpConnectionParams.setSoTimeout(params, 150000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
HttpPost httppost = new HttpPost(url);
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString = "";
try
{
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
ResponseHandler<String> rh = new ResponseHandler<String>()
{
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException
{
HttpEntity entity = response.getEntity();
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString = httpClient.execute(httppost, rh);
Log.d("Response is here....", "responseString : " + responseString);
}
catch (Exception e)
{
e.printStackTrace();
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
仍然出现错误:
javax.net.ssl.SSLHandshakeException: Connection closed by peer 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.Connection.connectTls(Connection.java:235) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:199) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at com.android.okhttp.Connection.connect(Connection.java:172) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.OkHttpClient.connectAndSetOwner(OkHttpClient.java:130) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329) 01-24 10:28:03.182 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246) 01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457) 01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126) 01-24 10:28:03.183 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89) 01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java) 01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46) 01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68) 01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err:
at srd.gshelp.GSSoapConWSDL.javaHit(GSSoapConWSDL.java:180) 01-24 10:28:03.184 32251-474/com.neosoft.meconnect W/System.err: at srd.gshelp.GSSoapConWSDL.access[=20=](GSSoapConWSDL.java:144) 01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err: at srd.gshelp.GSSoapConWSDL$TaskAsync.doInBackground(GSSoapConWSDL.java:215) 01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err:
at srd.gshelp.GSSoapConWSDL$TaskAsync.doInBackground(GSSoapConWSDL.java:1) 01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err:
at android.os.AsyncTask.call(AsyncTask.java:304) 01-24 10:28:03.185 32251-474/com.neosoft.meconnect W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 01-24 10:28:03.186 32251-474/com.neosoft.meconnect W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 01-24 10:28:03.186 32251-474/com.neosoft.meconnect W/System.err:
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 01-24 10:28:03.186 32251-474/com.neosoft.meconnect W/System.err:
at java.lang.Thread.run(Thread.java:761) 01-24 10:28:03.187 32251-474/com.neosoft.meconnect W/System.err: Suppressed: javax.net.ssl.SSLHandshakeException: Connection closed by peer
请帮忙。 提前致谢。
你用过Okhttp库吗? 这是一个非常好的网络调用库。您也可以处理此异常。
我遇到了类似的问题,我已经解决了这个问题:
public static OkHttpClient getHttpClientForFile() {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_0)
.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_DHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA)
.build();
return new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.readTimeout(3, TimeUnit.MINUTES)
.connectionSpecs(Collections.singletonList(spec))
.protocols(Arrays.asList(Protocol.HTTP_1_1))
.build();
}
我不知道它是否好用,但它对我有用。
您使用的 class SSLSocketFactory
可能会在 Play 商店发布应用程序后产生错误,或者 Play 商店可能会警告您更改代码。
您可以从 https://github.com/square/okhttp 找到 Okhttp
库。
确保您已从服务器端设置 TLS 启用。
我遇到了同样的问题,同样的 javax.net.ssl.SSLHandshakeException
在 API 调用时抛出。
但就我而言,以下是问题
我的 device
是 connected
到 wifi
但 wifi 路由器 not
有 internet
连接然后 exception
是抛出