What is my ip - Java PDF Print E-mail
Written by Charles   
Tuesday, 27 October 2009 23:00

Sometimes it's useful to know what the outside world sees as your IP address when you're behind a router, maybe using NAT. You can do this by asking the site whatismyip.com. The following is a way to do that and the current recommended website address is used. Make sure it's still valid before you rely on this code! It's HERE

 

import java.net.*;
import java.io.*;

public class WIMI {

    public final static String WIP_SITE = "http://automation.whatismyip.com/n09230945.asp";

    // For demo
    public static void main(String[] args) {
        System.out.println(WIMI.whatIsMyIp());
    }

    public static String whatIsMyIp() {
        String result = null;
        InputStream in = null;
        try {
            URLConnection conn = new URL(WIP_SITE).openConnection();
            int length = Integer.valueOf(conn.getHeaderField("Content-Length"));
            byte[] buf = new byte[length];
            in = conn.getInputStream();
            in.read(buf);
            result =  new String(buf);
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        finally {
            if (in != nulltry { in.close(); } catch(IOException e) { /* ignore */ }
        }
        return result;
    }

}
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Monday, 02 January 2012 21:52 )