我正尝试通过SSL通过get/post Web服务调用来访问远程服务器。我使用阿帕奇的HttpClient以下方式:如何通过https解决Web服务回馈SSLHandshaking错误?
HttpClient client = new HttpClient();
client.getHostConfiguration().setProxy("my_host", 443);
Credentials defaultcreds = new UsernamePasswordCredentials("dev", "password");
client.getState().setCredentials(new AuthScope("my_host", 443, AuthScope.ANY_REALM), defaultcreds);
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.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
发生什么特定的SSLHandshakeException?请发布堆栈跟踪。另外,您要连接的主机使用哪种证书?它是由主要CA签署的,还是由其自己的CA签署的? – Syon
CA自己选择。它尚未添加到密钥库中,但我试图绕过证书,因为这将用于测试目的。 – worm