Android HTTPS 请求
我已经尝试了很多选择,我快要疯了。每次尝试发布到 URL 时,我都会继续收到 SSL 异常。
在使用 HttpWebRequest 的 C# 中,这就像梦想一样。
我得到的错误是:
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();
}
I have tried so many options, I'm going to go crazy. I continue to get an SSL exception every time I try to post to a URL.
This works like a dream in C# using an HttpWebRequest.
The errors I get are:
Not trusted server certificate
java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
I am trying the following approach now, but I have tried custom SocketFactories, everything. Please help!
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要考虑Android如何确定证书的有效性。当需要验证证书时,它将查看签名链。如果它可以在其顶部找到受信任的颁发机构,并且该证书不在吊销列表中,那么它将被信任。
为了减少耗时的查询,Android 捆绑了一系列它信任的常见 CA。正如您在评论中指出的,升级后错误消失了。这很可能是由于您使用的 CA 已添加到已发货的受信任 CA 列表中。
如果您信任该证书,则可以将其添加到受信任 CA 列表中。 接受的答案此问题提供了有关旧版本此过程的一些详细信息!较新的版本更有可能附带您需要的证书。对于较新的版本,您可以直接从 SD 卡安装证书。转到
设置 ->安全性。在
凭据存储
下,您将找到从设备存储安装
选项。只要您使用标准格式,您就应该能够安装您的证书!我的来源:HTTPS 和 SSL 的安全性 | Android 开发者
You need to consider how Android determines the validity of certificates. When it needs to verify a certificate, it will look at the chain of signatures. If it can find a trusted authority at its top, and the certificate not being on the revocation list, then it will be trusted.
To reduce time-intensive queries, Android comes bundled with a list of common CAs that it trusts. As you noted in the comments, the error disappeared when you upgraded. This is most likely due to the CA you were using being added to the list of shipped trusted CAs.
You can, if you trust the certificate, add it to this list of trusted CAs. The accepted answer to this question has some details on this procedure for older versions! Newer versions are more likely to come shipped with the certificates you will need. With newer versions, you can install certificates directly from your SD card. Go to
Settings -> Security
. UnderCredential storage
you will find the optionInstall from device storage
. As long as you are using a standard format, you should be able to install your certificate!My source: Security with HTTPS and SSL | Android Developers