我一直在使用以下代码连接到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:证书中的主机名不匹配
发布评论