Skip to content

Instantly share code, notes, and snippets.

@SriramKeerthi
Created November 9, 2015 09:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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