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

时间:2015-05-23 02:14:28

标签: java networking ip-address ipv6 ipv4

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

r.table('logs').indexCreate('compound', function(row) {
  return [row('userId'), row('createdAt')];
})
r.table('logs').between([1, firstTime], [1, secondTime], {index: 'compound'})

但是那段代码只给我公开的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+”只是为了找到我的IPV6地址是浪费。有没有人有我计划使用的更好的解决方案?

此外,当我尝试使用“https://wtfismyip.com/text”代替“http://checkip.amazonaws.com”时,我收到安全例外[由前导“https”引起]

将https更改为http可修复安全性异常,但更新后的代码仍然无法正常运行:

    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);

此外,试图从谷歌搜索“我的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。我得到的只是:

    {
        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)
    }

1 个答案:

答案 0 :(得分:0)

我计划的解决方案是使用以下指令:

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

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

更新:此解决方案适用于使用java

从HTTPS站点获取IPV6地址