Java SSLException:证书中的主机名不匹配

编程入门 行业动态 更新时间:2024-10-20 03:20:15
本文介绍了Java SSLException:证书中的主机名不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用以下代码连接到Google的一项服务。这段代码在我的本地机器上工作正常:

I have been using the following code to connect to one of google's service. This code worked fine on my local machine :

HttpClient client=new DefaultHttpClient(); HttpPost post = new HttpPost("www.google/accounts/ClientLogin"); post.setEntity(new UrlEncodedFormEntity(myData)); HttpResponse response = client.execute(post);

我将此代码放在生产环境中,该环境已阻止Google。根据要求,他们允许我访问IP:74.125.236.52(这是Google的IP之一),允许与Google服务器进行通信。我编辑了我的hosts文件也添加了这个条目。

I put this code in a production environment, which had blocked Google. On request, they allowed communication with Google server by allowing me to accessing an IP : 74.125.236.52 - which is one of Google's IPs. I edited my hosts file to add this entry too.

我仍然无法访问URL,我想知道为什么。所以我用以下代码替换了上面的代码:

Still I could not access the URL, which I wonder why. So I replaced the above code with :

HttpPost post = new HttpPost("74.125.236.52/accounts/ClientLogin");

现在我收到这样的错误:

Now I get an error like this :

javax.ssl.SSLException:证书中的主机名不匹配:< 74.125.236.52> !=< www.google>

javax.ssl.SSLException: hostname in certificate didn't match: <74.125.236.52> != <www.google>

我想这是因为Google有多个IP。我不能让网络管理员允许我访问所有这些IP - 我甚至可能没有得到这整个列表。

I guess this is because Google has multiple IPs. I cant ask the network admin to allow me access to all those IPs - I may not even get this entire list.

我现在该怎么办? Java级别有解决方法吗?或者它完全掌握在网络人手中?

What should I do now ? Is there a workaround at Java level ? Or is it totally in hands of the network guy ?

推荐答案

感谢Vineet Reynolds。您提供的链接持有很多用户评论 - 其中一个我在绝望中尝试并且它有所帮助。我添加了这个方法:

Thanks Vineet Reynolds. The link you provided held a lot of user comments - one of which I tried in desperation and it helped. I added this method :

// Do not do this in production!!! HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){ public boolean verify(String string,SSLSession ssls) { return true; } });

这对我来说似乎很好,不过我知道这个解决方案是暂时的。我正在与网络人员一起确定我的主机文件被忽略的原因。

This seems fine for me now, though I know this solution is temporary. I am working with the network people to identify why my hosts file is being ignored.

更多推荐

Java SSLException:证书中的主机名不匹配

本文发布于:2023-11-23 20:45:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   主机名   证书   Java   SSLException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!