Skip to content

Instantly share code, notes, and snippets.

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
You can’t perform that action at this time.