使用jersey客户端通过HTTPS调用api时,我收到错误handshake_failure 。 如果该站点作为HTTP托管,那么它正常工作。 使用HTTPS时,我还关闭了服务器证书有效性检查。
@Test
public void GetMember2(){
try{
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
Date today1 = new Date(); // default window is 1 hour
String stringToday1 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(today1);
Client client = Client.create(configureClient());
WebResource webResource = client.resource("https://test.abcd.net/Members/72771583886/promotions?startDate=12122015");
ClientResponse response = webResource.accept("application/xml")
.type("application/xml")
.header("Date", stringToday1)
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
public static ClientConfig configureClient() {
TrustManager[ ] certs = new TrustManager[ ] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(null, certs, new SecureRandom());
} catch (java.security.GeneralSecurityException ex) {
}
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
ClientConfig config = new DefaultClientConfig();
try {
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
},
ctx
));
} catch(Exception e) {
}
return config;
}
错误:com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException:收到致命警报: handshake_failure at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155) 在com.sun.jersey.api.client.Client.handle(Client.java:652)at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)at at com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74) 在 com.sun.jersey.api.client.WebResource $ Builder.get(WebResource.java:509) 在Frameworks.Prototype2.GetLoyaltyMember2(Prototype2.java:106)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)
答案 0 :(得分:0)
握手可能由于各种原因而失败,而不仅仅是证书验证问题。 它可以是:
找出问题所在的最佳方法是安装Wireshark并查看握手消息。然后根据从服务器发送到客户端的SSL警报消息,您可以获得有关SSL握手失败的更多信息,具体发生在哪里。
此链接也可能有所帮助:Received fatal alert: handshake_failure through SSLHandshakeException
答案 1 :(得分:0)
暂时打开Java网络库的调试,以查看您获得的SSL错误类型:-Djavax.net.debug=all
答案 2 :(得分:0)
从oracle网站下载jce_policy-8.zip并解压得到以下2个JAR文件。
local_policy.jar US_export_policy.jar
在您的 JVM 中更新上述 2 项政策,即在我的情况下 /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/security
它应该可以工作。