Java - How can I prefer IPv6?

2019-03-01 13:18发布

问题:

I want to get the content of http://en.wikipedia.org using java.net.URL's openConnection() method.

I find that en.wikiepdia.org has both IPv4 and IPv6 address:

$ host en.wikipedia.org
en.wikipedia.org is an alias for wikipedia-lb.wikimedia.org.
wikipedia-lb.wikimedia.org is an alias for wikipedia-lb.eqiad.wikimedia.org.
wikipedia-lb.eqiad.wikimedia.org has address 208.80.154.225
wikipedia-lb.eqiad.wikimedia.org has IPv6 address 2620:0:861:ed1a::1

How can I access that site using IPv6 address?

回答1:

-Djava.net.preferIPv6Addresses=true

This jvm argument will work.



回答2:

IPv6 in Java is transparent and automatic. Just provide an an IPv6 address and you'll be good to go. Also check that your jvm does NOT have the following set to true

-Djava.net.preferIPv4Stack=true

More details here:



标签: java ipv6