gpt4 book ai didi

java - 使用 jersey 客户端通过 HTTPS 调用 api 时出现错误 handshake_failure。我已经关闭了服务器证书有效性检查

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:47 26 4
gpt4 key购买 nike

我在使用 jersey 客户端通过 HTTPS 调用 api 时收到error 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;
}

Error: com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155) at com.sun.jersey.api.client.Client.handle(Client.java:652) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509) at Frameworks.Prototype2.GetLoyaltyMember2(Prototype2.java:106) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

最佳答案

暂时打开 Java 网络库的调试以查看您遇到的是哪种 SSL 错误:-Djavax.net.debug=all

关于java - 使用 jersey 客户端通过 HTTPS 调用 api 时出现错误 handshake_failure。我已经关闭了服务器证书有效性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com