如何使HTTPS SOAP请求绕过Java中的SSL证书

时间:2014-03-03 02:02:57

标签: java ssl https ksoap2

我正在尝试使用Java控制台应用程序中的Ksoap发出HTTPS请求。我需要向LAN网络上的以下本地URL发出SOAP请求:

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。错误发生在第androidHttpTransport.call(SOAP_ACTION, envelope);行,导致SoapFault

如何使用Ksoap进行HTTPS调用

1 个答案:

答案 0 :(得分:1)

我知道这个问题是2岁,但是我必须找到类似问题的解决方法,这样可能对某人有所帮助。 以下是我在通过HTTPS调用SOAP服务时绕过SSL证书所做的工作:

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