Java URL Connection getGlobalAddress(String url)

Here you can find the source of getGlobalAddress(String url)

Description

Finds this computer's global IP address

License

Open Source License

Parameter

Parameter Description
url the url to find the global IP

Return

The global IP address, or null if a problem occurred

Declaration

public static Inet4Address getGlobalAddress(String url) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.// ww w. j a  v  a  2 s  . c o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**
     * Finds this computer's global IP address
     * 
     * @param url
     *            the url to find the global IP
     * @return The global IP address, or null if a problem occurred
     */
    public static Inet4Address getGlobalAddress(String url) {
        try {
            URLConnection uc = new URL(url).openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            return (Inet4Address) InetAddress.getByName(br.readLine());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. getCookies(URLConnection conn, Map> store)
  2. getDataFromServer(URL url)
  3. getDefaultUrlConnection(URL url)
  4. getFeedReader(URL feedUrl)
  5. getFromUrl(String url)
  6. getGlobalIPAddress(URL automationPage)
  7. getHeaderFieldLong(URLConnection conn, String name, long Default)
  8. getHTML(String url, boolean removeNonLatinChars)
  9. getHTML(URL url)