Created
November 9, 2015 09:50
-
-
Save SriramKeerthi/4fbb8821ad5b75fd0c80 to your computer and use it in GitHub Desktop.
Fetch IP address from http://caffinc.com/myip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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