Java getting my IP address

前端 未结 10 2410
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 16:53

I am trying to get my Internet IP address in Java but I keep getting my local address (ie: 127.0.0.1), when my IP address is 192.168.0.xxx

I am using the line:

10条回答
  •  悲哀的现实
    2020-11-27 17:25

    Let's ask AWS

    URL url = new URL("http://checkip.amazonaws.com/");
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    System.out.println(br.readLine());
    

    EDIT

    Before you downvote, I'm well aware this is not a java solution. Its a general solution for any programming language. The other solutions don't work as well for me. Also I believe the easier way of knowing your IP is to go on the internet. It can be any site, the server can return back your client ip that it got in the request. You can set up your own endpoint for it.

提交回复
热议问题