2011-10-06 188 views
4

我已经尝试了这么多选项,我要疯了。每次尝试发布到网址时,我都会继续收到SSL异常。Android HTTPS请求

这就像在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(); 
    } 
+0

在过去,我不得不把证书添加到Java密钥存储,以使这样的工作请求。 – mikey

+0

@mikey,我如何找到我通过的证书? – Benny

+0

我不相信你通过了一个证书。你需要确保服务器给你的证书是可信的。 C#和HttpRequest验证对不同证书存储区的https请求,而不是java。这似乎是一个很好的资源:http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/ – mikey

回答

1

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

为了减少耗时的查询,Android捆绑了一个它信任的常用CA列表。正如您在评论中提到的那样,升级时错误消失了。这很可能是由于您使用的CA被添加到运输的受信任CA的列表中。

如果您信任证书,则可以将其添加到受信任的CA列表中。 accepted answerthis question对于旧版本的这个过程有一些细节!较新的版本更可能附带您需要的证书。使用更新的版本,您可以直接从SD卡安装证书。去Settings -> Security。在Credential storage下,您会看到选项Install from device storage。只要你使用标准格式,你应该能够安装你的证书!

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