【问题标题】:Android HTTPS RequestAndroid HTTPS 请求
【发布时间】: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

标签: android ssl https


【解决方案1】:

您需要考虑 Android 如何确定证书的有效性。当它需要验证证书时,它会查看签名链。如果它可以在其顶部找到一个受信任的机构,并且该证书不在吊销列表中,那么它将是受信任的。

为了减少耗时的查询,Android 捆绑了一个它信任的常见 CA 列表。正如您在 cmets 中所述,升级时错误消失了。这很可能是因为您使用的 CA 已添加到已发布的受信任 CA 列表中。

如果您信任证书,则可以将其添加到此受信任 CA 列表中。 accepted answerthis question 提供了有关旧版本此过程的一些详细信息!较新的版本更有可能随附您需要的证书。对于较新的版本,您可以直接从 SD 卡安装证书。转到Settings -&gt; Security。在Credential storage 下,您将找到Install from device storage 选项。只要您使用标准格式,您应该可以安装您的证书!

我的来源:Security with HTTPS and SSL | Android Developers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多