作为正在进行的传奇的一部分,让我的应用获得Apple审核小组的批准,我碰到了另一个路障。InetAddress.getAllByName()抛出UnknownHostException
正如标题所示,当拨打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://开头” 似乎返回相同的,单一的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的,但后来我担心我以后使用HttpsURLConnection的会失败(我的连接必须是HTTPS)
testUrl = new URL("https://artatlas.io");
testConn = (HttpsURLConnection) testUrl.openConnection();
我知道一个HttpsURLConnection
使用InetAddress实例来形成它的连接,所以问题是它使用什么过程来解析URL字符串,它是否会删除协议?这里的正确方法是什么?
分离主机名和协议对我来说是一个重要的实现,谢谢。因此,我们可以安全地假设,当我创建一个HttpsURLConnection并将它传递给一个包含协议的URL时,它将不会失败(因为Java在构造InetAddress对象时不知道包含协议)? – Rohan
当然。 “URL”与主机名不同。有了一个URL你需要提供一个协议。没有关于它的假设。 – Kayaman
谢谢你解释说,这很有意义。我已经接受了这个答案,我也会赞成你,但是我似乎在互联网上有点短暂! – Rohan