External IP Address of the client

吃可爱长大的小学妹 提交于 2019-12-03 14:41:17

If your client is using NAT (network address translation) it may not have an external address. 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. At home I use NAT via a server so this laptop I'm typing on has no external address. The closest thing is what is returned from 'whatismyip', my server address, through which I may sometimes forward ports that go to my laptop.

Running "whatismyip" actions on code run on the server is only going to give you the server address.

Also,

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. However, the proxy may include the requesting client IP in a special HTTP header.

<%
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! (in a string like XXX.XXX.XXX.XXX)

The code for the website:

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

does not work anymore...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!