如何解决“SSLHANDSHAKE异常”以及如何在httpClient post方法中维护“Cookies”。我已将一些会话参数“nameValuePair”和cookie传递给http客户端post方法。 在“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
答案 0 :(得分:2)
看起来服务器没有有效的证书。可能服务器的Root-CA未包含在您的java cacert中。
也许这会有所帮助:http://blogs.oracle.com/gc/entry/unable_to_find_valid_certification