gpt4 book ai didi

java - 如何解决 SSLHandshake 异常?

转载 作者:可可西里 更新时间:2023-11-01 16:34:48 28 4
gpt4 key购买 nike

如何解决“SSLHANDSHAKE 异常”以及如何在 httpClient post 方法中维护“Cookies”。我已将一些 session 参数“nameValuePair”和 cookie 传递给 http 客户端发布方法。在“httpClient.executeMethod(postMethod)”中发布所有参数后,它不会用于下一个“postMethod.getResponseBodyAsString();”在它显示一些异常之后。

try {
initialState = new HttpState();
httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(25000);
httpClient.getParams().setParameter("http.socket.timeout", new Integer(25000));
httpClient.getHostConfiguration().setProxy("127.0.0.1",8888);
// Properties systemProperties = System.getProperties();
// systemProperties.put("proxySet", "true");
// systemProperties.put("http.proxyHost", "127.0.0.1");
// systemProperties.put("http.proxyPort", 8888);
try {
httpClient.getParams().setAuthenticationPreemptive(true);
// httpClient.getState().setProxyCredentials(null, null, new UsernamePasswordCredentials(null, null));
} catch (Exception ex) {
ex.printStackTrace();
}
//httpClient.getParams().setCookiePolicy(CookiePolicy.ACCEPT_ALL);

cookies = httpClient.getState().getCookies();
} catch (Exception ex) {
}
try {
postMethod = new PostMethod(pageUrl);
postMethod.addParameter("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");
postMethod.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
postMethod.addRequestHeader("Accept-Language", "en-us,en;q=0.5");
postMethod.addRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
postMethod.addRequestHeader("KeepAlive", "true");
postMethod.setFollowRedirects(FollowRedirect);
postMethod.setDoAuthentication(DoAuthentication);

if (referer != null) {
postMethod.addRequestHeader("referer", referer);
}
if (cookieArr != null) {
cookies = new Cookie[cookieArr.length];
for (int i = 0; i < cookieArr.length; i++) {
cookies[i] = new Cookie(".linkedin.com", cookieArr[i][0], cookieArr[i][0], "/", null, true);
}
}
initialState.addCookies(cookies);
int postDataLength = postData.length;
NameValuePair[] nameValuePair = new NameValuePair[postDataLength];
for (int i = 0; i < postDataLength; i++) {
nameValuePair[i] = new NameValuePair(postData[i][0], postData[i][1]);
}

postMethod.addParameters(nameValuePair);
httpClient.executeMethod(postMethod);
cookies = httpClient.getState().getCookies();
pageSouce = postMethod.getResponseBodyAsString();
} catch (Exception ex) {
ex.printStackTrace();

} finally {
postMethod.releaseConnection();
}

但是运行时出现如下异常:

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

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

最佳答案

看起来服务器没有有效的证书。可能服务器的 Root-CA 不包含在您的 java cacert 中。

也许这会有所帮助:http://blogs.oracle.com/gc/entry/unable_to_find_valid_certification

关于java - 如何解决 SSLHandshake 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8837391/

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