简体   繁体   English

外部 IP 客户端地址

[英]External IP Address of the client

Sounds funny, but how can I get the external IP address from a client?听起来很有趣,但是如何从客户端获取外部 IP 地址?

I tried few things, but didn't work for me.我尝试了几件事,但没有为我工作。

in first place I tried首先我试过

request.getRemoteAddr()

and I am getting the result as: 0:0:0:0:0:0:0:1我得到的结果是:0:0:0:0:0:0:0:1

in second place I tried第二我试过

InetAddress ip = InetAddress.getLocalHost();
ip.getHostAddress());

and I am getting the result as: 127.0.0.1我得到的结果是:127.0.0.1

in third place I tried第三名我试过

        URL whatismyip = new URL("http://checkip.dyndns.org:8245/");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

        String IPStrOld = inIP.readLine(); //IP as a String
        String IPStrNewest = IPStrOld.replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
        String IPStr = IPStrNewest.replace("</body></html>", "");

but I get the external IP of the server only但我只得到服务器的外部 IP

and for the last place最后一个地方

        URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
        String ip = inIP.readLine();

this is the same, I get the external IP of the server only这个是一样的,我只拿到服务器的外部IP

So, what's the deal?那么,有什么关系呢?

If your client is using NAT (network address translation) it may not have an external address.如果您的客户端使用NAT (网络地址转换),它可能没有外部地址。 Most often, in my experience, this is the case.大多数情况下,根据我的经验,情况就是这样。 At work, my web requests go through a proxy so the web server can only determine this address.在工作中,我的 web 通过代理请求 go 所以 web 服务器只能确定这个地址。 At home I use NAT via a server so this laptop I'm typing on has no external address.在家里,我通过服务器使用 NAT,所以我正在输入的这台笔记本电脑没有外部地址。 The closest thing is what is returned from 'whatismyip', my server address, through which I may sometimes forward ports that go to my laptop.最接近的是从我的服务器地址“whatismyip”返回的内容,有时我可以通过它将 go 的端口转发到我的笔记本电脑。

Running "whatismyip" actions on code run on the server is only going to give you the server address.在服务器上运行的代码上运行“whatismyip”操作只会为您提供服务器地址。

Also,还,

http://www.rgagnon.com/javadetails/java-0363.html http://www.rgagnon.com/javadetails/java-0363.html

From that link:从那个链接:

<%
out.print( request.getRemoteAddr() );
out.print( request.getRemoteHost() );
%>

You may not get the real client IP if a the client is behind a proxy, you will get the IP of the proxy and not the client.如果客户端在代理后面,您可能无法获得真正的客户端 IP,您将获得代理的 IP 而不是客户端。 However, the proxy may include the requesting client IP in a special HTTP header.但是,代理可能会在特殊的 HTTP header 中包含请求客户端 IP。

<%
out.print( request.getHeader("x-forwarded-for") );
%>

The code:编码:

    URL whatismyip = new URL("http://checkip.dyndns.org:8245/");
    BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
    String IPStrOld = inIP.readLine(); //IP as a String
    String IPStrNewest = IPStrOld.replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
    String IPStr = IPStrNewest.replace("</body></html>", "");

works fine for me.对我来说很好。 I got my router IP address with it.我得到了我的路由器 IP 地址。 (in a string like XXX.XXX.XXX.XXX) (在类似 XXX.XXX.XXX.XXX 的字符串中)

The code for the website:网站代码:

http://automation.whatismyip.com/n09230945.asp http://automation.whatismyip.com/n09230945.asp

does not work anymore...不再工作了......

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

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