Rohan
Rohan

Reputation: 456

InetAddress.getAllByName() throws UnknownHostException

As part of an ongoing saga to get my app approved by the Apple review team, I have come up against another road block.

As the title suggests, when calling InetAddress.getAllByName("https://artatlas.io") an UnknownHostException is thrown. This only happens during their testing process. When I test my app on my local NAT64 network (as suggested by Apple); the error never occurs, and the app works as intended.

The code I am running:

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();
}

What I've discovered is, anything I append with "https://" seems to return the same, single IP address:

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

I could just get rid of the https, but then I'm concerned my later use of a HttpsURLConnection will fail (my connection MUST be https)

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

I know that a HttpsURLConnection uses an InetAddress instance to form its connection, so the question is what process does it use to parse the URL string, does it remove the protocol? What's the correct approach here?

Upvotes: 1

Views: 2055

Answers (1)

Kayaman
Kayaman

Reputation: 73528

The hostname shouldn't include a protocol. The host is the same, no matter what protocol you intend to use with it. Whether the later HTTPS connection fails or not, InetAddress.getAllByName() is unrelated to it (it doesn't and can't guarantee success or failure).

You're dealing with DNS only at this point, so it's just foo.com or 123.45.67.89 or an IPv6 address.

Upvotes: 3

Related Questions