【发布时间】:2011-10-26 21:35:38
【问题描述】:
在使用 Apache HttpComponents HttpClient 库 (4.0.2) 时,我遇到了证书未正确验证的问题。该证书对域名有效(我们称之为 example.com),但它会根据 IP 地址进行验证:
证书中的主机名不匹配: !=
我的连接代码是:
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
String url = "https://www.example.com";
HttpGet get = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(get);
String response = EntityUtils.toString(httpResponse.getEntity()).trim();
证书本身在通过网络浏览器连接时显示为有效,并且对我要连接的域名有效:
CN = *.example.com
证书也被添加到 Java 密钥库中(使用常规 HttpsURLConnection 测试)。
知道为什么这段代码使用 IP 地址而不是域名吗?
【问题讨论】:
-
所以当涉及到 SSL 时,域无法解析为 IP。因此,您不能使用 IP 证书来保护域。很高兴知道。谢谢!
标签: java httpclient apache-httpcomponents