我正在尝试在 Java 控制台应用程序中使用 Ksoap
发出 HTTPS 请求。我需要向我的 LAN 网络上的以下本地 URL 发出 SOAP 请求:
https://192.168.0.43:7002/Examples/TestService?WSDL
由于它是一个 HTTPS 请求,我需要绕过证书并访问 web 服务。我仅在测试环境中使用网络服务。
这是我使用 ksoap 发送 SOAP 请求的 java 代码。
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 中提到的修改了代码
但是,每当我运行代码时,我都会收到代码为 1000 的安全策略错误
。错误发生在 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");
关于java - 如何绕过 Java 中的 SSL 证书发出 HTTPS SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137469/