【发布时间】:2011-12-01 23:49:52
【问题描述】:
我已经尝试了很多选择,我要发疯了。每次尝试发布到 URL 时,我都会继续收到 SSL 异常。
这就像在 C# 中使用 HttpWebRequest 实现的梦想。
我得到的错误是:
Not trusted server certificate
java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
我现在正在尝试以下方法,但我已经尝试了自定义 SocketFactories,一切。请帮忙!
final String httpsURL = "https://...";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost(httpsURL);
//authentication block:
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("mail", username));
nvps.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity p_entity = null;
try {
p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httppost.setEntity(p_entity);
//sending the request and retrieving the response:
HttpResponse response = null;
try {
response = client.execute(httppost, _context);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity responseEntity = response.getEntity();
//handling the response: responseEntity.getContent() is your InputStream
try {
final InputSource inputSource = new InputSource(responseEntity.getContent());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
【问题讨论】:
-
在过去,我必须将证书添加到 java 密钥库中才能发出这样的请求。
-
@mikey,我如何找到我通过的证书?
-
我不相信您正在通过证书。您需要确保服务器提供给您的证书是可信的。 C# 和 HttpRequest 针对不同于 java 的证书存储验证 https 请求。这似乎是一个很好的资源:@987654321@
-
请注意,他提到在构建您的请求时创建一个证书存储文件,但我没有服务器证书。
-
我自己不必这样做。这有点取决于您是否可以获得 CA 证书,因为添加 CA 的证书可能会更好。但是,如果需要,您可以使用此工具将证书从站点中提取出来:addons.mozilla.org/en-US/firefox/addon/cert-viewer-plus