2014-03-03 61 views
1

我试图在Java控制台应用程序中使用Ksoap发出HTTPS请求。我需要让我的LAN网络上的SOAP请求发送到以下本地网址:如何使HTTPS SOAP请求绕过Java中的SSL证书

https://192.168.0.43:7002/Examples/TestService?WSDL

既然是HTTPS请求,我需要绕过证书并击中了web服务。我只在测试环境中使用webservice。

这是我的java代码,它使用kso​​ap发送SOAP请求。

public class ConnectHttps { 
    public static void main(String[] args) throws Exception { 
    /* 
* fix for 
* Exception in thread "main" javax.net.ssl.SSLHandshakeException: 
*  sun.security.validator.ValidatorException: 
*   PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
*    unable to find valid certification path to requested target 
*/ 
TrustManager[] trustAllCerts = new TrustManager[] { 
    new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
     return null; 
     } 

     public void checkClientTrusted(X509Certificate[] certs, String authType) { } 

     public void checkServerTrusted(X509Certificate[] certs, String authType) { } 

    } 
}; 

SSLContext sc = SSLContext.getInstance("SSL"); 
sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

// Create all-trusting host name verifier 
HostnameVerifier allHostsValid = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
}; 
// Install the all-trusting host verifier 
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 
/* 
* end of the fix 
*/ 

//send SOAP Request 
    URL url = new URL("https://192.168.0.43:7002/Examples/TestService?WSDL"); 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request); 


    //HttpsTransportSE androidHttpTransport= new KeepAliveHttpsTransportSE("https://192.168.0.43", 7002, "TestService", 9000); 

    try{ 

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     androidHttpTransport.call(SOAP_ACTION, envelope); 

     SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 

     if (envelope.bodyIn instanceof SoapFault) { 
      String str= ((SoapFault) envelope.bodyIn).faultstring; 

      System.out.println("" +str); 

     } else { 
      SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 
      System.out.println("WS"+ ""+ String.valueOf(resultsRequestSOAP)); 
     } 
    }catch(Exception ex) 
    { 
     System.out.println("Error:" +ex.getMessage()); 
     ex.printStackTrace(); 

    }*/ 



    } 
} 

注:我已经修改了代码,每在这个link

但是提到,每当我运行代码,我得到一个security policy error with code 1000。在导致SoapFault

我应该怎么做使用KSOAP

回答

1

我知道这个问题为2岁的HTTPS电话线androidHttpTransport.call(SOAP_ACTION, envelope);出现错误,但是我必须找到一个类似的问题的解决方法,因此它可能帮助他人。 以下是我在通过HTTPS调用SOAP服务时绕过SSL证书的过程:

BaseWLSSLAdapter.setStrictCheckingDefault(false); 
SSLAdapterFactory.getDefaultFactory().createSSLAdapter(); 
System.setProperty("org.apache.axis.components.net.SecureSocketFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");