最初のアプローチはここにあります:
<?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"
network_security_config
をres/xml/network_security_config
に含めました。ca
証明書はres/raw/my_ca.pem
に含まれています
2番目のアプローチはここにあります:
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:接続がピアによって閉じられました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 $ 1.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 $ 0(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 $ 2.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:接続がピアによって閉じられました
親切に助けてください。前もって感謝します。
サーバー側からTLS enabledを設定していることを確認してください。
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();
}
良いかどうかはわかりませんが、うまくいきました。
使用したクラスSSLSocketFactory
は、Playストアでアプリを公開した後にエラーを生成する可能性があります。または、Playストアがコードを変更するよう警告する場合があります。
Okhttp
ライブラリは https://github.com/square/okhttp から見つけることができます。
私は同じ問題に直面しました、同じjavax.net.ssl.SSLHandshakeException
は、API呼び出し時にスローしています。
しかし私の場合、次のことが問題でした
私のdevice
はconnected
に対してwifi
でしたが、wifiルーターはnot
接続を持っているinternet
でしたが、exception
がスローされました