Android HTTPS 请求

发布于 2024-12-08 02:56:26 字数 1831 浏览 0 评论 0原文

我已经尝试了很多选择,我快要疯了。每次尝试发布到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃梦幻 2024-12-15 02:56:26

您需要考虑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. Under Credential storage you will find the option Install 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文