Reputation: 8787
Method String domain = request.getRemoteHost();
returns 0:0:0:0:0:0:0:1
IPv6 address. LAN card is configured to support IPv6 protocol, Firefox as well: network.dns.disableIPv6 false
, and Java 1.8.0_141-b15 64-bit is installed. But if I enter http://0:0:0:0:0:0:0:1
it shows an error or shows the results on Google (??). If I enter localhost
or 127.0.0.1
it shows my project's webpage. How can it be fixed? My goal is not to disable IPv6, but to support both IPv4 and IPv6 protocols.
Proposed solution was to add brackets: http://[0:0:0:0:0:0:0:1]
. And it's working. But now we have to add brackets manually, and only if protocol is IPv6. Perhaps getRemoteHost()
should be updated to better support IPv6?
I found information about literal IPv6 addresses in network resource identifiers here:
Colon (:) characters in IPv6 addresses may conflict with the established syntax of resource identifiers, such as URIs and URLs. The colon has traditionally been used to terminate the host path before a port number.[8] To alleviate this conflict, literal IPv6 addresses are enclosed in square brackets in such resource identifiers, for example:
http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/
When the URL also contains a port number the notation is:
https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/
So it seems Java problem...? Now working on IPv6 regex..
Upvotes: 2
Views: 4298
Reputation: 298479
If you ask Java for a host name, you'll get a host name, not a URI fragment. There is nothing wrong with that. If you actually want a URI, you should ask for a URI:
String host = "::1", path = "/";
URI uri = new URI("http", host, path, null);
System.out.println("URI: " + uri);
will print
URI: http://[::1]/
But if you construct the URI string manually instead, it will be your responsibility to add brackets when necessary.
Upvotes: 4
Reputation: 8787
Found an easy solution:
public static boolean isIPv6(String IP) throws UnknownHostException {
InetAddress address = InetAddress.getByName(IP);
return address instanceof Inet6Address;
}
But it would be very nice to have a method getRemoteHost(true | false)
to get IPv6 address with brackets or not.
Upvotes: 0