pau*_*aul 21 java https exception
尝试使用https连接到服务器时,我收到此'HTTPS hostname wrong:'错误.我的网址看起来像这样
https://sub.domain.com/tamnode/webapps/app/servlet.
Run Code Online (Sandbox Code Playgroud)
我使用以下代码连接
// Create a URLConnection object for a URL
URL url = new URL(requestedURL);
HttpURLConnection.setFollowRedirects(false);
// connect
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
OutputStreamWriter wr = new OutputStreamWriter(connection
.getOutputStream());
Run Code Online (Sandbox Code Playgroud)
但后来得到一个错误
IOException: HTTPS hostname wrong: should be <sub.domain.com>.
at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing
....
Run Code Online (Sandbox Code Playgroud)
这是过去曾经工作但不再有效的代码.系统架构已经发生了一些变化,但我需要在接近负责人之前获得更多数据.
什么可能导致此错误?我可以关闭URLSpoofing检查吗?
cle*_*tus 18
看起来domain.com的SSL证书已经提供给sub.domain.com.或者,更有可能的是,domain.com已被重命名为sub.domain.com而未更新SSL证书.
vkr*_*mer 13
克莱图斯对可能的原因是正确的.
有一种方法可以关闭欺骗检查.
您可以创建一个实现HostnameVerifier的对象,该对象在比"通常"更多的情况下返回true.
您可以通过在问题代码中的连接对象上调用setHostnameVerifier来替换默认的HostnameVerifier .
这个答案是"受到启发":http://www.java-samples.com/showtutorial.php?tutorialid = 211
我发现此链接与此查询相关联:http://www.google.com/search? q = http + https + hostname+wrong+should+be
还有一点需要注意:在你这样做之前要三思.您将在客户端和服务器组件之间的安全性中创建可利用的弱点.
我得到了这个例外 - java.io.IOException: HTTPS hostname wrong: should be <localhost>.
我的解决方案是我更改了自签名证书并制作了CN=localhost.
要么
将证书域名添加cn=<domain-name>到主机文件中,该文件可能位于c:/ windows/system32/drivers/etc/...
小智 5
以下代码解决了我的问题
static {
//for localhost testing only
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier() {
@Override
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("your_domain")) {
return true;
}
return false;
}
});
}
Run Code Online (Sandbox Code Playgroud)