[英]How to make HTTPS SOAP request bypassing SSL certificate in Java
我正在尝试在Java控制台应用程序中使用Ksoap
发出HTTPS请求。 我需要对我的LAN网络上的以下本地URL发出SOAP请求:
https://192.168.0.43:7002/Examples/TestService?WSDL
由于这是HTTPS请求,因此我需要绕过证书并访问Web服务。 我仅在测试环境中使用Web服务。
这是我的Java代码,它使用ksoap发送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();
}*/
}
}
注意:我已修改此链接中提到的代码
但是,每当我运行代码时,都会收到security policy error with code 1000
的security policy error with code 1000
。 错误发生在androidHttpTransport.call(SOAP_ACTION, envelope);
行androidHttpTransport.call(SOAP_ACTION, envelope);
导致SoapFault
如何使用Ksoap进行HTTPS调用
我知道这个问题已有2年历史了,但是我不得不为类似的问题找到解决方法,以便对某些人有所帮助。 这是我通过HTTPS调用SOAP服务时绕过SSL证书所做的事情:
BaseWLSSLAdapter.setStrictCheckingDefault(false);
SSLAdapterFactory.getDefaultFactory().createSSLAdapter();
System.setProperty("org.apache.axis.components.net.SecureSocketFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.