带有可信证书的KSaop2 HttpsTransportSE的SSLHandshakeException

时间:2012-06-22 11:28:46

标签: android tomcat ssl ksoap2

我的ksoap2 webservice和webService-client适用于HttpTransportSE。现在我想将SSL与受信任的证书一起使用。要在Tomcat SSL上使用Web服务,我在Axis2.xml中添加了Https Transport Reciver,我认为webservice可以工作。这是我的SSL网络服务:

<https://myURL.de:8443/WebProject_KomplexeObjekte_SSL/services/HelloWorldWS?wsdl>?

下一步是在我的客户端中将httptransportSE(URL)更改为httpstransportSE(HOST,PORT,FILE,TIMEOUT)。 这是我的客户:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsServiceConnectionSE;
import org.ksoap2.transport.HttpsTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class WSClientKomplexeObjekteActivity extends Activity {

private static final String SOAP_ACTION = "http://ws.chathura.com/getCategoryObject";
private static final String METHOD_NAME = "getCategoryObject";
private static final String NAMESPACE = "http://ws.chathura.com";
private static final String NAMESPACE2 = "http://ws.chathura.com/xsd";
private static final String HOST = "myURL.de";
private static final int PORT = 8443;
private static final String FILE = "/WebProject_KomplexeObjekte_SSL/services/HelloWorldWS?wsdl";
private static final int TIMEOUT = 1000;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.textview1);


    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    Category C = new Category();
    C.setCategoryId(1);
    C.setDescription("Client Beschreibung");
    C.setName("Client Name"); 

    Request.addProperty("obj", C);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(Request);
    envelope.addMapping(NAMESPACE2, C.getClass().getSimpleName(), C.getClass());

    HttpsTransportSE androidHttpsTransport = new HttpsTransportSE(HOST, PORT, FILE, TIMEOUT);

    Category ans = null;
    try {
        androidHttpsTransport.call(SOAP_ACTION, envelope);
        ans = (Category)envelope.getResponse();
        tv.setText("CategoryId: " + ans.getCategoryId() + "\nName: " + ans.getName() + "\nDescription: " + ans.getDescription()); 
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}


}

现在我在Logcat中得到了这个例外:

W/System.err(619): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

由于这个异常,我尝试包含一个密钥库,其中包含我的tomcat上用于ssl的证书。您可以在评论中看到此代码。 那没有成功。我得到以下例外:

E/AndroidRuntime(654): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bachelor.marcel/de.bachelor.marcel.WSClientKomplexeObjekteActivity}: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:1)

好的......我自己解决了这个问题。客户端代码不是问题。它工作得很好。唯一的问题是在服务器端。 Android不相信我的服务器,因为我忘了包含根证书。因此,如果您遇到同样的问题,可以使用http://www.sslshopper.com/ssl-checker.html#

检查您的ssl服务器

答案 1 :(得分:0)

您可以使用此课程绕过证书:

public class FakeX509TrustManager 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 FakeX509TrustManager() };
        }

        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());
    }

}