InetAddress.getAllByName() 抛出 UnknownHostException

InetAddress.getAllByName() throws UnknownHostException

作为让我的应用程序获得 Apple 审核团队批准的持续传奇的一部分,我遇到了另一个障碍。

如标题所示,当调用 InetAddress.getAllByName("https://artatlas.io") 时抛出 UnknownHostException。这只发生在他们的测试过程中。当我在本地 NAT64 网络上测试我的应用程序时(按照 Apple 的建议);错误从未发生,应用程序按预期运行。

我的代码是运行:

System.setProperty("java.net.preferIPv6Addresses", "true");
System.setProperty("networkaddress.cache.negative.ttl", "0");
InetAddress[] addrs;

try {
    addrs = InetAddress.getAllByName("https://artatlas.io");
    String addresses = "";
    for(InetAddress addr: addrs){
        addresses += addr + "\n";
    }
    System.out.println("Addresses: " + addresses + "\n");
} catch (IOException e1) {
    e1.printStackTrace();
}

我发现的是,我附加 "https://" 的任何内容似乎 return 相同的单个 IP 地址:

Addresses: https://artatlas.io/122.150.5.20
Addresses: https://google.com/122.150.5.20
Addresses: https://www.google.com/122.150.5.20

我可以去掉 https,但我担心以后使用 HttpsURL连接会失败(我的连接必须是 https)

testUrl = new URL("https://artatlas.io");
testConn = (HttpsURLConnection) testUrl.openConnection();

我知道 HttpsURLConnection 使用 InetAddress 实例来形成它的连接,所以问题是它使用什么进程来解析 URL 字符串,它是否删除了协议?这里的正确方法是什么?

主机名不应包含协议。主机是相同的,无论您打算使用什么协议。后面的HTTPS连接失败与否,InetAddress.getAllByName()与其无关(不保证也不能保证成功或失败)。

此时您只处理 DNS,因此它只是 foo.com123.45.67.89 或 IPv6 地址。