gpt4 book ai didi

java - 如何成功获取外部IP

转载 作者:搜寻专家 更新时间:2023-11-01 03:57:17 24 4
gpt4 key购买 nike

看完后:Getting the 'external' IP address in Java

代码:

public static void main(String[] args) throws IOException
{
URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
}

我以为我是赢家,但我收到以下错误

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://automation.whatismyip.com/n09230945.asp
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at getIP.main(getIP.java:12)

我认为这是因为服务器响应不够快,有没有办法确保它能获得外部 ip?

编辑:好的,所以它被拒绝了,其他任何人都知道可以执行相同功能的另一个网站

最佳答案

    public static void main(String[] args) throws 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();
System.out.println(str);
}

关于java - 如何成功获取外部IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10322506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com