Java URL Connection getGlobalIPAddress(URL automationPage)

Here you can find the source of getGlobalIPAddress(URL automationPage)

Description

Returns the global (not the local) IP address of the host by using the given automation page.

License

Open Source License

Parameter

Parameter Description
automationPage An automation page

Return

The global IP address of the host

Declaration

public static String getGlobalIPAddress(URL automationPage) 

Method Source Code


//package com.java2s;
//License from project: MIT License 

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**//from  www.ja va  2  s.com
     * Returns the global (not the local) IP address of the host by using the
     * given automation page. <br/><br/>
     * 
     * <i>Annotation:</i> For example <a
     * href="http://www.whatismyip.com/">www.whatismyip.com</a> offers an
     * automation page.
     * 
     * @param automationPage An automation page
     * @return The global IP address of the host
     */
    public static String getGlobalIPAddress(URL automationPage) {
        InputStream inputStream = null;

        try {
            URLConnection urlConnection = automationPage.openConnection();

            int contentLength = Integer.valueOf(urlConnection.getHeaderField("Content-Length"));

            byte[] buffer = new byte[contentLength];

            inputStream = urlConnection.getInputStream();

            inputStream.read(buffer);

            return new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();

            return null;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. getDataFromServer(URL url)
  2. getDefaultUrlConnection(URL url)
  3. getFeedReader(URL feedUrl)
  4. getFromUrl(String url)
  5. getGlobalAddress(String url)
  6. getHeaderFieldLong(URLConnection conn, String name, long Default)
  7. getHTML(String url, boolean removeNonLatinChars)
  8. getHTML(URL url)
  9. getHttpGetContent(String strUrl, String charSet)