我正在尝试使用ssl上的ksoap2库进行soap连接,但无法使其正常工作。我尝试了许多解决方案,我可以在互联网上找到但没有运气。
如果有人可以帮助我,我们将非常感激。以下是我的代码:
错误
onPreExecute
02-18 10:47:19.722 3876-3898/com.example I/LOGINACTIVITY﹕ doInBackground
02-18 10:47:19.722 3876-3898/com.example I/LOGINACTIVITY﹕ making Soap request ...
02-18 10:47:19.791 3876-3898/com.example W/System.err﹕ javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:306)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:197)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:151)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:130)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.sendData(HttpTransportSE.java:292)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:184)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.example.Utils.SoapRequest.makeSoapRequestLogin(SoapRequest.java:47)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.example.LoginActivity.makeRequest(LoginActivity.java:81)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.example.LoginActivity$AsyncCallWS.doInBackground(LoginActivity.java:147)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at com.example.LoginActivity$AsyncCallWS.doInBackground(LoginActivity.java:142)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-18 10:47:19.792 3876-3898/com.example W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:318)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:219)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:113)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:525)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302)
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ ... 24 more
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
02-18 10:47:19.793 3876-3898/com.example W/System.err﹕ ... 30 more
02-18 10:47:19.794 3876-3898/com.example I/LOGINACTIVITY﹕ Result -
02-18 10:47:19.841 3876-3876/com.example I/LOGINACTIVITY﹕ onPostExecute
02-18 10:47:19.841 3876-3876/com.example I/LOGINACTIVITY﹕ Result :
02-18 10:47:19.841 3876-3876/com.example I/LOGINACTIVITY﹕ Incorrect Username and Password !!
活动
private final String NAMESPACE = "https://myhost.com/";
private final String HOST = "myhost.com";
private final String URL = "https://myhost.com/AndroidWFC/AndroidWS.asmx";
private final String WS_OPS = "/MobileWFC/MobileWS.asmx";
private final String SOAP_ACTION = "https://myhost.com/AndroidWFC/UserControl";
private final String METHOD_NAME = "UserControl";
/* Only relevant part of activity is included. */
// calling constructor
SoapRequest soapRequest = new SoapRequest(NAMESPACE, URL, HOST, WS_OPS , SOAP_ACTION, METHOD_NAME);
// calling soapRequest method
result = soapRequest.makeSoapRequestLogin(txtUsername.getText().toString(), txtPassword.getText().toString(), txtCode.getText().toString());
SoapRequest类
package com.example.Utils;
import android.util.Log;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsServiceConnectionSE;
import org.ksoap2.transport.HttpsTransportSE;
/**
* Created by Sanjay Kumar on 2/16/2015.
*/
public class SoapRequest {
private String NAMESPACE = "";
private String URL = "";
private String HOST = "";
private String WS_OPS = "";
private String SOAP_ACTION = "";
private String METHOD_NAME = "";
public SoapRequest(String NAMESPACE, String URL, String HOST, String WS_OPS, String SOAP_ACTION, String METHOD_NAME) {
this.NAMESPACE = NAMESPACE;
this.URL = URL;
this.HOST = HOST;
this.WS_OPS = WS_OPS;
this.SOAP_ACTION = SOAP_ACTION;
this.METHOD_NAME = METHOD_NAME;
}
public String makeSoapRequestLogin(String user, String pass, String code) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(makePropertyInfo("Username", user));
request.addProperty(makePropertyInfo("Password", pass));
request.addProperty(makePropertyInfo("userdealer", code));
String result = "";
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
//HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
HttpsTransportSE androidHttpTransport = new HttpsTransportSE(HOST,443,WS_OPS,2000);
androidHttpTransport.debug = true;
SslRequest.allowAllSSL();
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public PropertyInfo makePropertyInfo(String propertyInfoName, String propertyInfoValue) {
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName(propertyInfoName);
propertyInfo.setValue(propertyInfoValue);
propertyInfo.setType(double.class);
return propertyInfo;
}
}
SslRequest类
package com.example.Utils;
import android.util.Log;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* Created by Sanjay Kumar on 2/18/2015.
*/
public class SslRequest implements X509TrustManager {
private static TrustManager[] trustManagers;
private static final X509Certificate[] _AcceptedIssuers = new
X509Certificate[]{};
@Override
public void checkClientTrusted(X509Certificate[] chain, String
authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String
authType) throws CertificateException {
}
public boolean isClientTrusted(X509Certificate[] chain) {
return true;
}
public boolean isServerTrusted(X509Certificate[] chain) {
return true;
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return _AcceptedIssuers;
}
public static void allowAllSSL() {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = null;
if (trustManagers == null) {
trustManagers = new TrustManager[]{new SslRequest()};
}
try {
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
}