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:
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.
My Solution which only returns 1 Ip4 address:
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp() || iface.isVirtual() || iface.isPointToPoint())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
final String ip = addr.getHostAddress();
if(Inet4Address.class == addr.getClass()) return ip;
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return null;
Had the same problem, found the solution on this page: http://mrhawy.blogspot.it/2012/05/how-to-get-your-external-ip-address-in.html
public String getIpAddress() throws MalformedURLException, IOException {
URL myIP = new URL("http://api.externalip.net/ip/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
}
had some troubles with this code on the long run, several times in a week the server just won't reply.
new solution:
public static String getIpAddress()
{
URL myIP;
try {
myIP = new URL("http://api.externalip.net/ip/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e)
{
try
{
myIP = new URL("http://myip.dnsomatic.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e1)
{
try {
myIP = new URL("http://icanhazip.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return null;
}
//This program is find your exact LAN(Local Machine on which your are //runing this program) IP Address
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetMyIPAddress {
public static void main(String gks[]) throws SocketException{
Enumeration e = NetworkInterface.getNetworkInterfaces();
int ctr=0;
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements() && ctr<3)
{ ctr++;
if(ctr==3)
break;
InetAddress i = (InetAddress) ee.nextElement();
if(ctr==2)
System.out.println(i.getHostAddress());
}
}
}
}
Another option for default network interface, just I was trying 5 min ago and saw your question :)
InetAddress[] localaddr;
try {
localaddr = InetAddress.getAllByName("host.name");
for(int i = 0; i < localaddr.length; i++){
System.out.println("\n" + localaddr[i].getHostAddress());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
You need to get the jar of jsoup here add the jar of jsoup in your java project and interpret this lines of code and you will get your ip adress,
Document doc = Jsoup.connect("https://whatismyipaddress.com/").timeout(10000).get() ;
Elements el = doc.select("div#section_left") ;
Element e = el.select("a").get(
System.out.println(e.text());