【问题标题】:Trust all certificates? X509Certificate信任所有证书? X509证书
【发布时间】:2011-09-26 20:49:42
【问题描述】:

我有以下通过 HTTPS 连接的代码,但在尝试连接时出现以下错误。

WARN/System.err(9456): javax.net.ssl.SSLHandshakeException: org.bouncycastle.jce.exception.ExtCertPathValidatorException:无法验证证书签名。

我该如何解决这个问题? 信任所有证书?我怎么做? 没问题,因为我只连接到同一台服务器。 仅供参考,我已经在我的应用中将 EasyX509TrustManager 实现为一个类。

提前谢谢你。

 try {
            HttpClient client = new DefaultHttpClient();
            // Setting up parameters 
            SchemeRegistry schemeRegistry = new SchemeRegistry(); 
            // http scheme 
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
            // https scheme 
            schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); 

            HttpParams params = new BasicHttpParams(); 
            params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); 
            params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); 
            params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); 
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 

            ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); 

            DefaultHttpClient httpClient = new DefaultHttpClient(cm, client.getParams()); 


            // Example send HttpsPost request 
            final String url = "https://www.xxxx.com/web/restricted/form/formelement"; 

            // Do the HTTPS Post
            HttpPost httpPost = new HttpPost(url); 

                ArrayList<NameValuePair> postParameters; 
            postParameters = new ArrayList<NameValuePair>(2); 
            postParameters.add(new BasicNameValuePair("usr_name", username)); 
            postParameters.add(new BasicNameValuePair("usr_password", password));

            System.out.println(postParameters); 

            HttpResponse response = httpClient.execute(httpPost);
            Log.w("Response ","Status line : "+ response.toString());

嗨,彼得,

谢谢您的回答,我听从了您的建议,无法验证的消息消失了。但是现在我收到以下消息:

09-26 23:47:20.381:WARN/ResponseProcessCookies(10704):Cookie 被拒绝:“BasicClientCookie[version=0,name=ObFormLoginCookie,domain=www.kpn.com,path=/web/restricted/form?表单元素=512663,到期=空]"。非法路径属性“/web/restricted/form?formelement=512663”。来源路径:“/web/restricted/form/formelement=512663” 09-26 23:47:20.461:警告/响应(10704):状态行:org.apache.http.message.BasicHttpResponse@407afb98

所以标题中似乎有问题?

【问题讨论】:

    标签: android https x509certificate


    【解决方案1】:

    不要信任所有证书,这是一个非常非常糟糕的主意。使用您的服务器证书创建一个密钥库,放入应用程序的原始资产,并将其加载到 HttpClient 的SSLSocketFactory。然后在SchemeRegistry中为https注册这个工厂。

    【讨论】:

      【解决方案2】:

      您需要提供自己的TrustManager,它信任所有证书。

      看到这个答案:HTTPS and self-signed certificate issue

      【讨论】:

      • 嗨,彼得,请看我上面编辑过的消息。提前谢谢你。
      • 发送的 cookie 与 cookie 中定义的路径不匹配。有一个“?”而不是“/”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2015-09-30
      • 2012-09-28
      • 1970-01-01
      • 2018-06-30
      • 2013-12-20
      相关资源
      最近更新 更多