Skip to content

Instantly share code, notes, and snippets.

@SriramKeerthi
Created November 9, 2015 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SriramKeerthi/4fbb8821ad5b75fd0c80 to your computer and use it in GitHub Desktop.
Save SriramKeerthi/4fbb8821ad5b75fd0c80 to your computer and use it in GitHub Desktop.
Fetch IP address from http://caffinc.com/myip
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
/**
* Fetches the IP address of the machine from http://caffinc.com/myip
* @author Sriram
*/
public class MyIPDemo
{
public static void main(String[] args) throws Exception {
System.out.println( getIp() );
}
private static String getIp() throws IOException
{
try ( BufferedReader br = new BufferedReader(
new InputStreamReader( new URL( "http://caffinc.com/myip/" ).openConnection().getInputStream() ) ) ) {
String ip;
if ( ( ip = br.readLine() ) != null ) {
return ip;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment