我试图从 java 调用 heroku 的开发者 api,但出现以下异常:
Exception in thread "main" javax.net.ssl.SSLException: hostname in certificate didn't match: <50.19.233.255> != <*.heroku.com>
我的代码是这样的:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://api.heroku.com/apps");
String token = "d6d7ea6e-6e71-4f13-b0ff-ed9ee9d56c37";
request.addHeader("Authorization", "Bearer "+token);
HttpResponse response = client.execute(request);
如果我用 curl 尝试它,它工作正常:
curl "https://api.heroku.com/apps" -H"Authorization: Bearer d6d7ea6e-6e71-4f13-b0ff-ed9ee9d56c37"
为什么 java 代码对 curl 的行为不同?
附言我知道其他人已经问过这个问题,但所有的答案,例如:
https://stackoverflow.com/a/7266768
https://stackoverflow.com/a/3904473
https://stackoverflow.com/a/25356821
建议我应该覆盖证书主机名检查,这肯定违背了要点(当然不是生产就绪)?
最佳答案
此问题在 Apache HttpClient resolving domain to IP address and not matching certificate 中有描述. 它似乎是您使用的 HTTPClient 版本中的一个错误,它将目标 IP 而不是目标主机名与主题证书进行比较。请改用固定版本的 HTTPClient。
关于java - SSLException : hostname in certificate didn't match: <50. 19.233.255> != <*.heroku.com>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493872/