Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class GetIP {
- public String getIP()
- {
- String ip = "";
- try
- {
- URL url = new URL("https://www.google.co.in/search?q=what+is+my+ip");
- HttpURLConnection Conn = (HttpURLConnection)url.openConnection();
- 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");
- Conn.connect();
- InputStream InStream = Conn.getInputStream();
- InputStreamReader Isr = new InputStreamReader(InStream);
- BufferedReader Br = new BufferedReader(Isr);
- while((ip = Br.readLine())!= null)
- {
- if(ip.contains("Your public IP address is"))
- {
- Matcher matcher = Pattern.compile("(?i)(<em.*?>)(.+?)(</em>)").matcher(ip);
- matcher.find();
- ip = matcher.group(2);
- break;
- }
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return ip;
- }
- public static void main(String[] args) {
- GetIP ip = new GetIP();
- System.out.println(ip.getIP());
- }
- }
Add Comment
Please, Sign In to add comment