[英]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.