2015-06-09 98 views
0

我有一段代碼將有效負載發送到https端點(或應該)。我也有一個.pem格式的CA鏈,我在代碼中嘗試添加它,並使用它來執行POST。使用httpClient和cert.em發送https請求

HttpClient client = new HttpClient(); 
       Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
       String jsonString = gson.toJson(parentData); 
       Properties systemProps = System.getProperties(); 
       systemProps.put("javax.net.ssl.trustStore", "/Users/kaulk/Downloads/djca-2048.pem"); 
       systemProps.put("javax.net.ssl.trustStorePassword", "changeit"); 
       System.setProperty("javax.net.ssl.keyStoreType","pkcs12"); 
       System.setProperties(systemProps);    
       PostMethod method = new PostMethod("https://beta.fcm.fint.xxx.net/notify/BuildNotification"); 
       StringRequestEntity requestEntity = new StringRequestEntity(
           jsonString, 
           "application/json", 
           "UTF-8"); 
       method.setRequestEntity(requestEntity); 
       int statusCode = client.executeMethod(method); 

但它失敗,錯誤:

Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl) at java.security.Provider$Service.newInstance(Provider.java:1245) at sun.security.jca.GetInstance.getInstance(GetInstance.java:220) at sun.security.jca.GetInstance.getInstance(GetInstance.java:147) at javax.net.ssl.SSLContext.getInstance(SSLContext.java:125) at javax.net.ssl.SSLContext.getDefault(SSLContext.java:68) at javax.net.ssl.SSLSocketFactory.getDefault(SSLSocketFactory.java:102) ... 22 more Caused by: java.io.IOException: Invalid keystore format

任何原因?

回答

1

按照文件上的SSL性能

javax.net.ssl.trustStoreType - (Optional) For Java keystore file format, this property has the value jks (or JKS). You do not normally specify this property, because its default value is already jks.

嘗試設置javax.net.ssl.trustStoreType

你越來越經常引發的異常是由於潛在的錯誤。

這些設置也將幫助你獲得更多的信息,以解決 -Djavax.net.debug = SSL,或者至少-Djavax.net.debug = SSL,的KeyManager

的的storetype應根據證書文件導入 有用的帖子 - Java Exception on SSLSocket creation

+0

trustStoreType應該是什麼? pkcs12或jks? – Scooby

+0

它應該是默認的jks - http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#Customization –

+0

但它應該取決於您的證書文件格式 - https ://docs.oracle.com/cd/E29585_01/PlatformServices.61x/security/src/tsec_ssl_jsp_pkcs12.html –