2011-11-15 70 views
0

問題:SOAP請求:非法協議HTTPS的HTTP的URLConnection廠

我打電話一個SOAP web服務,但需要每次調用更改URL。但是,wsdl中的終點是內部地址,所以我正在嘗試自己編輯端點。假設這是正確的做法(?),我得到java.io.IOException:HTTP URLConnection工廠非法協議https,我正在努力解決。

代碼:

OtherCompanyWebService ws = new OtherCompanyWebService(); 
    OtherCompanyWebServicePortType port = ws.getOtherCompanyWebServiceHttpSoap11Endpoint(); 
    ServiceRequest serviceRequest = makeMyServiceRequest(); 

    BindingProvider bindingProvider = (BindingProvider) port; 
    bindingProvider.getRequestContext().put(
        BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
        "https://blah.blah.com/blah/services/blahWebService.blahWebServiceHttpSoap11Endpoint"); 

    Staff staff = port.getStaffData(serviceRequest).getStaff().getValue(); 

堆棧跟蹤:

javax.xml.ws.soap.SOAPFaultException: Could not send Message. 
     at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:199) 
     at $Proxy52.getStudentData(Unknown Source) 
     at uk.co.txttools.rm.service.RmServiceImpl.runRmJob(RmServiceImpl.java:209) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) 
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
     at $Proxy12.runRmJob(Unknown Source) 
     at uk.co.txttools.rm.quartz.RmJob.execute(RmJob.java:41) 
     at org.quartz.core.JobRunShell.run(JobRunShell.java:216) 
     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) 
Caused by: org.apache.cxf.interceptor.Fault: Could not send Message. 
     at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:48) 
     at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:220) 
     at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:296) 
     at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:242) 
     at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73) 
     at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:178) 
     ... 16 more 
Caused by: java.io.IOException: Illegal Protocol https for HTTP URLConnection Factory. 
     at org.apache.cxf.transport.http.HttpURLConnectionFactoryImpl.createConnection(HttpURLConnectionFactoryImpl.java:44) 
     at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:480) 
     at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46) 

回答

1

我已經找到解決的方法是很不理想,但似乎工作。如果您有更好的解決方案,請在此處發佈。

問題是在wsdl文件中,端點是http。發生此錯誤的原因是我將端點更改爲https。 (可能這是一個bug?)

爲了解決這個問題,我使用了wsdl文件的本地版本,並將兩個端點放在一個http和一個https中。然後,當我更改端點時,我要麼更改http或https,這取決於我需要http還是https。

0

當我收到此消息時,發現我沒有爲我的TLS客戶端(WS)設置SSL上下文。

如果代碼可以是任何幫助:

/** 
* Configure mutal TLS on WS given in parameter 
* 
* @param portType webservice 
* @throws Exception 
*/ 
public static void setTLS(Object ws) throws Exception { 

    if (tlsClientParameters == null) { 
     if (StringUtils.hasLength(ConfigUtils.getInstance().getProxyHost()) && StringUtils.hasLength(ConfigUtils.getInstance().getProxyPort())) { 
      System.setProperty("https.proxyHost", ConfigUtils.getInstance().getProxyHost()); 
      System.setProperty("https.proxyPort", ConfigUtils.getInstance().getProxyPort()); 
     } 

     X509Certificate[] certificatesChain = new X509Certificate[1]; 

     KeyManager[] managers = new KeyManager[1]; 

     Pkcs12 pkcs12 = new Pkcs12(); 
     pkcs12.login(new ClassPathResource(ConfigUtils.getInstance().getCertificateFileName()).getURI().getPath(), ConfigUtils.getInstance().getCertificatePassword()); 
     certificatesChain[0] = pkcs12.getCertificate(); 
     managers[0] = new SSLKeyManagers(certificatesChain, pkcs12.getKey()); 

     Client client = ClientProxy.getClient(portType); 
     SSLContext sslContext = SSLContext.getInstance("TLS"); 

     TrustManager[] trustManager = getServerTrustManager(); 

     sslContext.init(managers, trustManager, null); 
     HTTPConduit conduit = (HTTPConduit) client.getConduit(); 

     tlsClientParameters = new TLSClientParameters(); 
     if (trustManager != null) { 
      tlsClientParameters.setTrustManagers(trustManager); 
     } 
     tlsClientParameters.setKeyManagers(managers); 
     tlsClientParameters.setSSLSocketFactory(sslContext.getSocketFactory()); 

     tlsClientParameters.setDisableCNCheck(false); 

     conduit.setTlsClientParameters(tlsClientParameters); 

    } else { 

     Client client = ClientProxy.getClient(portType); 
     HTTPConduit conduit = (HTTPConduit) client.getConduit(); 

     conduit.setTlsClientParameters(tlsClientParameters); 
    } 
} 

/** 
* 
    * Gets the trust manager 
* 
* @return keystore 
* @throws NoSuchAlgorithmException 
* @throws KeyStoreException 
* @throws ResourceException 
* @throws IOException 
* @throws CertificateException 
*/ 
private static TrustManager[] getServerTrustManager() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, ResourceException { 

    TrustManagerFactory factory = TrustManagerFactory.getInstance("X.509"); 

    KeyStore trustStore = KeyStore.getInstance("JKS"); 

    trustStore.load(new ClassPathResource(ConfigUtils.getInstance().getServerKeyStoreFileName()).getInputStream(), ConfigUtils.getInstance().getServerKeystorePassword().toCharArray()); 

    factory.init(trustStore);  
    return factory.getTrustManagers(); 
}