Apache HttpClient 将域解析为 IP 地址且不匹配证书
使用 Apache HttpComponents HttpClient 库 (4.0.2) 时,我遇到了证书无法正确验证的问题。该证书对于域名(我们称之为 example.com)有效,但它是根据 IP 地址进行验证的:
证书中的主机名不匹配:<123.123.123.123> != <*.example.com>
我用于建立连接的代码是:
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 地址而不是域名吗?
When using the Apache HttpComponents HttpClient library (4.0.2) I'm having a problem where the certificate doesn't get validated properly. The certificate is valid for the domain name (let's call it example.com) however it's getting validated against the IP address instead:
hostname in certificate didn't match: <123.123.123.123> !=
<*.example.com>
My code for making the connection is:
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();
The certificate itself shows as valid when connecting through a web browser and is valid for the domain name I'm connecting to:
CN = *.example.com
The certificate is also added to the Java keystore (tested using regular HttpsURLConnection
).
Any ideas why this code uses the IP address instead of the domain name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎是 HttpClient 4.0.2 的已知错误 - https://issues.apache.org /jira/browse/HTTPCLIENT-996
该错误表明存在以下任一情况:
Appears to be a known bug with HttpClient 4.0.2 - https://issues.apache.org/jira/browse/HTTPCLIENT-996
The bug suggests any of the following: