AHow to get current IP in Java? -
i use code current ip:
public string getipaddress() throws nullpointerexception, malformedurlexception, ioexception { url whatismyip = new url("http://checkip.amazonaws.com"); bufferedreader in = null; try { in = new bufferedreader(new inputstreamreader( whatismyip.openstream())); string ip = in.readline(); return ip; } { if (in != null) { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
but return value incorrect.
what's wrong in code?
you can use will's code on thread : getting 'external' ip address in java
he use amazon service "external ip".
here code :
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.url; public class ipchecker { public static string getip() throws exception { url whatismyip = new url("http://checkip.amazonaws.com"); bufferedreader in = null; try { in = new bufferedreader(new inputstreamreader( whatismyip.openstream())); string ip = in.readline(); return ip; } { if (in != null) { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } } } }
all credits goes will.
Comments
Post a Comment