Java 콘솔 응용 프로그램에서 Ksoap
을 사용하여 HTTPS 요청을하려고합니다. 내 LAN 네트워크에 다음과 같은 지역의 URL에 SOAP 요청을해야합니다Java에서 SSL 인증서를 사용하지 않고 HTTPS SOAP 요청을 수행하는 방법
https://192.168.0.43:7002/Examples/TestService?WSDL
을 그것이 HTTPS 요청이기 때문에, 내가 인증서를 무시하고 웹 서비스를 공격해야합니다. 테스트 환경에서만 webservice를 사용하고 있습니다.
다음은 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
에 언급 된 따라 난 내가 코드를 실행할 때마다, 나는 security policy error with code 1000
을 얻고, 코드를 수정했습니다. 이 오류는 그러나 나는 비슷한 문제에 대한 해결 방법을 찾을 수 있었다, 나는 Ksoap