SSLException:证书中的主机名不匹配:<50.19.233.255> != <*.heroku.com>

SSLException: hostname in certificate didn't match: <50.19.233.255> != <*.heroku.com>

我试图从 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 的行为不同?

P.S。我知道其他人已经问过这个问题,但所有答案,例如:

建议我应该覆盖证书主机名检查,这肯定违背了要点(当然不是生产就绪)?

中描述了此问题。 它似乎是您使用的 HTTPClient 版本中的一个错误,它将目标 IP 而不是目标主机名与主题证书进行比较。请改用固定版本的 HTTPClient。