gpt4 book ai didi

java - 无法从 java 客户端调用 HTTPS 不受信任的 URL

转载 作者:行者123 更新时间:2023-12-01 15:36:22 26 4
gpt4 key购买 nike

我想从java调用一个https URL,它不被信任,而且它没有域名,只有IP(公共(public)或私有(private))。当我调用一个具有不受信任证书的域名的网址时,它可以工作。但对于 IP 地址来说,它不起作用。这是我得到的错误和源代码。你能告诉我问题是什么以及解决这个问题的方法吗?

谢谢!

错误:

java.io.IOException: HTTPS hostname wrong:  should be <xxx.xxx.xxx.xxx>

来源:

public static void main(String args[]){

StringBuffer param = new StringBuffer();
param.append("https://xxx.xxx.xxx.xxx/insert.php");
param.append("?a=a");
param.append("&b=c");
param.append("&c=c");
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

try{
URL url =new URL(param.toString());
URLConnection con = url.openConnection();
con.setAllowUserInteraction(true);
con.getInputStream();
BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer result = new StringBuffer();

while ((inputLine = in.readLine()) != null){
result.append(inputLine);
}
in.close();

System.out.println("Result=" + result.toString());
}catch(Exception ee){
System.out.print(ee);
}

}

最佳答案

与某些浏览器不同,Oracle 的主机名 validator 的 Java 实现遵循 RFC 2818 (HTTPS 规范)严格限于使用 IP 地址(而不是主机名)。特别是,主题备用名称扩展中必须有一个 IP 条目。

更多详细信息请参见this answer .

请注意,从评论链接的代码只是禁用任何证书验证,这将使其可能遭受中间人攻击:不要使用它!

关于java - 无法从 java 客户端调用 HTTPS 不受信任的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8784198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com