信任所有证书吗? X509证书
我有以下通过 HTTPS 连接的代码,但在连接尝试期间出现以下错误。
警告/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?formelement=512663,expiry=null]”。非法路径属性“/web/restricted/form?formelement=512663”。来源路径:“/web/restricted/form/formelement=512663” 09-26 23:47:20.461:警告/响应(10704):状态行:org.apache.http.message.BasicHttpResponse@407afb98
那么标题中似乎有问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要相信所有证书,这是一个非常非常糟糕的主意。使用服务器的证书创建密钥库,放入应用程序的原始资产,然后将其加载到 HttpClient 的 SSLSocketFactory。然后在
SchemeRegistry
中为 https 注册该工厂。Don't trust all certificates, that is a very, very bad idea. Create a keystore with your server's certificate, put in the app's raw assets, and load it into HttpClient's SSLSocketFactory. Then register this factory for https in the
SchemeRegistry
.您需要提供自己的信任所有证书的
TrustManager
。请参阅此答案:HTTPS 和自签名证书问题
You need to provide your own
TrustManager
which trusts all certificates.See this answer: HTTPS and self-signed certificate issue