How to get external IP successfully

前端 未结 9 1277
耶瑟儿~
耶瑟儿~ 2020-12-03 16:16

After reading: Getting the 'external' IP address in Java

code:

public static void main(String[] args) throws IOException
{
    URL whatismyip         


        
9条回答
  •  无人及你
    2020-12-03 16:51

    Using the Check IP address link on AWS worked for me.Please note that MalformedURLException,IOException are to be added as well

    public String getPublicIpAddress() throws MalformedURLException,IOException {
    
        URL connection = new URL("http://checkip.amazonaws.com/");
        URLConnection con = connection.openConnection();
        String str = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        str = reader.readLine();
    
    
        return str;
    }
    

提交回复
热议问题