繁体   English   中英

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

[英]How do I get my public IPV6 address from inside my Java Application?

通常,当我想从程序内部获取公共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地址,如果我在Google搜索中输入“我的IP是什么”,我会得到一个不同的IPV6地址,而不是IPV4地址。 从我的应用程序内部,我想获取IPV6地址,但我认为解析整个Google网页(“ https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF)很浪费-8#q = what + is + my + ip + “只是在其中找到我的IPV6地址。 有谁有我计划使用的更好的解决方案?

另外,当我尝试使用“ https://wtfismyip.com/text ”代替“ http://checkip.amazonaws.com ”时,出现安全异常[由领先的“ 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)
    }

此外,尝试从Google搜索“我的IP是什么”中提取IPV6地址-https ://www.google.com/webhp?sourceid= chrome- instant&ion=1&espv=2&ie= UTF-8# q=什么+ is + my + ip + “-根本没有给我一个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&&

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

http://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html

要获取https网页https://wtfismyip.com/ (或该站点上的其他HTTPS页面-如果使用http则不能获得任何内容)的内容。 然后使用这些内容来推断您的ipv4和ipv6地址。 会看看是否有效。

更新:此解决方案适用于使用Java从HTTPS站点获取IPV6地址的情况

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM