java - 如何从 Java 应用程序内部获取公共(public) IPV6 地址?

标签 java networking ip-address ipv6 ipv4

通常,当我想要从程序内部获取公共(public) 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);

但是该代码只提供了我的公共(public) 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&&

最佳答案

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

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

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

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

关于java - 如何从 Java 应用程序内部获取公共(public) IPV6 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408218/

相关文章:

java - 基于 SOAP 的 Web 服务中的身份验证和角色 (Java)

android - Lollipop 中的多网络

android - 如何通过wifi查找设备的IP地址?

java - 如何在java中将模式dd-MMM-yy的字符串日期值转换为模式dd/MM/yyyy的日期对象

java - NetBeans IDE 代码完成快捷方式

java - 用于按浮点参数对对象数组列表进行排序的比较器

java - 网络编程远未达到基准

performance - 在 32 核/10Gbit 主机上调优 Netty

Android 在托管热点时查找设备的 ip 地址

Haskell 将套接字绑定(bind)到特定 IP