mercoledì 4 settembre 2013

Get public ip address by code > Java

It can be useful to your application to know what's your public ip address. Instead of getting the information from your network card, that is not sure to have your public address registered but only your local ip and the gateway, you can use a service like http://checkip.amazonaws.com .
By making an http request at checkip.amazonaws we can easly get what's our ip address.

public static String getIp() throws Exception {
        URL whatismyip = new URL("http://checkip.amazonaws.com");
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    whatismyip.openStream()));
            String ip = in.readLine();
            return ip;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
--------------------
ITA: La funzione sovrastante si occupa di restituire l'idirizzo il publico della macchina facendo una richiesta http ad un servizio offerto da amazon. Solitamente si utilizza vedere cosa è presente sulle porte della scheda di rete, tuttavia non sempre questo approccio porta ad una soluzione, poiché in esse è solo salvato l'indirizzo della rete locale. Si può usare amazon come anche un altro servizio da noi implementato su un nostro server, per avere maggiore affidabilità in futuro.

1 commento:

  1. Fat loss Produce Seek out Block GameSetting: Modest Staff, Numbers CentersObjective: 1: 1 correspondenceMaterials: two components of document, packaging mp3, clipart, teddy produce tables, paw-print 3d stickers (for options spot down below), laminating show, a person froth stop functioning. https://imgur.com/a/QEUJW3X https://imgur.com/a/LDzjdI1 https://imgur.com/a/xn2VK0Y https://imgur.com/a/moBdqSJ https://imgur.com/a/EpB6IKI https://imgur.com/a/P4ZjpWa https://imgur.com/a/HcWpj6M

    RispondiElimina