【问题标题】:How do I get my public IPV6 address from inside my Java Application?如何从我的 Java 应用程序中获取我的公共 IPV6 地址?
【发布时间】:2015-08-05 03:45:20
【问题描述】:

通常,当我想从我的程序中获取我的公共 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 地址,但我认为在“https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=what+is+my+ip+”解析整个 Google 网页只是为了在其中找到我的 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 是什么” - “https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=what+is+my+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&&

【问题讨论】:

  • checkip.amazonaws.com 似乎只有 ipv4 接口,所以它只接受来自 ipv4 地址的连接。您也可以查看wtfismyip.com/text。它也应该识别 v6 和 v4。
  • 我尝试了“wtfismyip.com/text”,起初我遇到了这个重大的安全异常,然后我得到了一个与“checkip.amazonaws.com”给我的 IPV4 地址相同的地址。请参阅问题底部发布的更新代码。
  • 你能让代码与wtfismyip.com/text一起工作吗?
  • 由于 IPv6 不应该使用任何 NAT,您可以只获取分配给主机的 IPv6 地址列表,而不是询问外部服务。
  • 把它作为一个答案,如果它有效,我会给你最好的答案。

标签: java networking ip-address ipv6 ipv4


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 2011-07-16
    • 2021-12-03
    • 2017-06-25
    • 2012-05-25
    • 2016-05-27
    • 2023-04-09
    • 2017-06-21
    相关资源
    最近更新 更多