3x5w4rup

Get IP from Google

Jul 14th, 2014
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class GetIP {
  7.     public String getIP()
  8.     {
  9.         String ip = "";
  10.         try
  11.         {
  12.             URL url = new URL("https://www.google.co.in/search?q=what+is+my+ip");
  13.             HttpURLConnection Conn = (HttpURLConnection)url.openConnection();
  14.             Conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
  15.             Conn.connect();
  16.             InputStream InStream = Conn.getInputStream();
  17.             InputStreamReader Isr = new InputStreamReader(InStream);
  18.             BufferedReader Br = new BufferedReader(Isr);
  19.             while((ip = Br.readLine())!= null)
  20.             {
  21.                 if(ip.contains("Your public IP address is"))
  22.                 {
  23.                     Matcher matcher = Pattern.compile("(?i)(<em.*?>)(.+?)(</em>)").matcher(ip);
  24.                     matcher.find();
  25.                     ip = matcher.group(2);
  26.                     break;
  27.                 }
  28.             }
  29.         }
  30.         catch(Exception e)
  31.         {
  32.             e.printStackTrace();
  33.         }
  34.         return ip;
  35.     }
  36.     public static void main(String[] args) {
  37.         GetIP ip = new GetIP();
  38.         System.out.println(ip.getIP());
  39.     }
  40.  
  41. }
Add Comment
Please, Sign In to add comment