如何从Java应用程序内部获取公共IPV6地址?

如何从Java应用程序内部获取公共IPV6地址?,java,networking,ip-address,ipv6,ipv4,Java,Networking,Ip Address,Ipv6,Ipv4,通常,当我想从程序中获取公共IP时,我会复制并粘贴以下代码: URL whatismyip = new URL("http://checkip.amazonaws.com"); InputStreamReader in = new InputStreamReader(whatismyip.openStream()); BufferedReader in = new BufferedReader(in); final String ip = in.readLine(

通常,当我想从程序中获取公共IP时,我会复制并粘贴以下代码:

    URL whatismyip = new URL("http://checkip.amazonaws.com");
    InputStreamReader in = new InputStreamReader(whatismyip.openStream());
    BufferedReader in = new BufferedReader(in);
    final String ip = in.readLine(); //you get the IP as a String
    System.out.println("You IP is: " + ip);
但该代码只提供了我的公共IPV4地址,如果我在谷歌搜索中键入“what is my ip”,我会得到一个不同的IPV6地址,而不是IPV4地址。从我的应用程序中,我想得到IPv6地址,但是我认为在“谷歌”中解析整个网页只是为了寻找它的IPv6地址。有谁有比我计划使用的更好的解决方案吗

另外,当我尝试使用“”代替“”时,我得到一个安全异常[由前面的“https”引起]

将https更改为http会修复安全异常,但更新的代码仍然无法正常工作:

    {
        final URL whatismyip = new URL("http://checkip.amazonaws.com");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                whatismyip.openStream()));
        final String ip = in.readLine(); //you get the IP as a String
       System.out.println("Your IPV4 IP is: " + ip);
    }
    {

        final URL whatismyip2 = new URL("http://wtfismyip.com/text");
        final BufferedReader in2 = new BufferedReader(new InputStreamReader(
                whatismyip2.openStream()));
        final String ip2 = in2.readLine(); //you get the IP as a String
        Application.printerr("Your IPV6 IP is: " + ip2); // Your IPV6 IP is the same at your IPV4 (wrong because when I check in the web browser is different)
    }
此外,试图从谷歌搜索“what is my ip”(我的ip是什么)中提取IPV6地址,甚至根本没有给我ip地址,ipv4或IPV6都没有。我得到的只是:

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content=
"Search the world's information, including webpages, images, videos and more. Google has many special features 
to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots">
<meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'SAxgVa73L4_FgwSy34LgCw',kEXPI:'3700256,3700366,4017578,4026111,4029815,4031300,4032032,4032500,4032521,
4032631,4032643,4032645,4032677,4032926,4033142,4033184,4033191,4033307,4033344,4034425,4035816,4035881,4035980,
4036005,4036345,4036464,4036486,4036531,4036539,4036665,4036896,4037457,4037538,4037611,8300096,8500394,8500851,
8501248,8501279,8501295,8501351,8501406,8501489,8501497,10200083,10201180,10201191',authuser:0,kSID:'c9c918f0_10'};
google.kHL='en';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)
a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=
a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){};google.time=
function(){return(new Date).getTime()};google.log=function(a,b,e,f,l){var d=new Image,h=google.lc,g=google.li,c="",m=
google.ls||"";d.onerror=d.onload=d.onabort=function(){delete h[g]};h[g]=d;if(!e&&-1==b.search("&ei=")){
var k=google.getEI(f),c="&ei="+k;-1==b.search("&lei=")&&((f=google.getLEI(f))?c+="&lei="+f:k!=google.kEI&&(
c+="&lei="+google.kEI))}a=e||"/"+(l||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+c+m+"&zx="+google.time();/^
http:/i.test(a)&&google.https()?(google.ml(Error("a"),!1,{src:a,glmm:1}),delete h[g]):(window.google&&window.google.vel&&

谷歌(function(){window.Google={kEI:'SAxgVa73L4_FgwSy34LgCw',kEXPI:'370025637003664017578402611114029815403300203240325004032521,
4032631,4032643,4032645,4032677,4032926,4033142,4033184,4033191,4033307,4033344,4034425,4035816,4035881,4035980,
4036005,4036345,4036464,4036486,4036531,4036539,4036665,4036896,4037457,4037538,4037611,8300096,8500394,8500851,
85011248850127985012958501351850140685014898501497102000831020118010201191',authuser:0,kSID:'c9c918f0_10';
google.kHL='en';}();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&(!a.getAttribute | |!(b=a.getAttribute(“eid”));)
a=a.parentNode;返回b | | google.kEI};google.getLEI=function(a){for(var b=null;a&(!a.getAttribute | |!(b=a.getAttribute(“leid”));)a=
a、 parentNode;return b};google.https=function(){return“https:==window.location.protocol};google.ml=function(){};google.time=
function(){return(new Date).getTime()};google.log=function(a,b,e,f,l){var d=new Image,h=google.lc,g=google.li,c=”“,m=
google.ls | |“”;d.onerror=d.onload=d.onabort=function(){delete h[g]};h[g]=d;if(!e&&1==b.search(&ei=“)){
var k=google.getEI(f),c=“&ei=“+k;”-1==b.search(&lei=“)&((f=google.getLEI(f))?c+=”&lei=“+f:k!=google.kEI&&(
c++=“&lei=“+google.kEI”)}a=e | |“/”+(l | |“gen|u 204”)+“?atyp=i&ct=“+a+”&cad=“+b+c+m+”&zx=“+google.time()”/^
http:/i.test(a)和&google.https()?(google.ml(Error(“a”),!1,{src:a,glmm:1}),删除h[g]:(window.google和window.google.vel)&&

我计划的解决方案是使用以下说明:

获取https网页(或该站点上的其他https网页-如果我使用http,则无法获取任何内容)的内容。然后使用这些内容推断您的ipv4和ipv6地址。将查看它是否有效


更新:此解决方案可用于使用java从HTTPS站点获取IPV6地址。checkip.amazonaws.com似乎只有ipv4接口,因此它只接受来自ipv4地址的连接。您也可以进行检查。它还应识别v6和v4。我尝试了“”起初我遇到了一个大的安全异常,然后我得到了一个与IPV4地址“checkip.amazonaws.com”相同的地址给了我。请参阅问题底部发布的更新代码。你能让代码正常工作吗?因为IPv6不应该使用任何NAT,你可以只获取分配给主机的IPv6地址列表,而不必询问外部服务。回答这个问题,如果可行,我将给出你的最佳答案。