gpt4 book ai didi

java.net.UnknownHostException :www. google.com

转载 作者:行者123 更新时间:2023-12-01 12:56:43 36 4
gpt4 key购买 nike

我正在开发一个健全性检查网络应用程序。我尝试使用 HttpUrlConnection 方法获取 url 响应,但收到 UnknownHostException。

 System.setProperty("java.net.preferIPv4Stack" , "true");
String[] uat_targetUrls={"https://www.google.com"};
String[] uat_targetResponse=new String[uat_targetUrls.length];

HttpURLConnection httpUrlConn;
httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[i])
.openConnection();

httpUrlConn.setRequestMethod("GET");


httpUrlConn.setConnectTimeout(30000);
httpUrlConn.setReadTimeout(30000);



if(httpUrlConn.getResponseCode()==200)
uat_targetResponse[i]="UP";
else
uat_targetResponse[i]="DOWN";

执行此操作时,我收到各种 url 的 UnknownHostException。谁可以帮我这个事。我正在使用 Eclipse IDE。这是我收到的错误:

java.net.UnknownHostException: www.google.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)

谢谢。

最佳答案

问题一定是您计算机上的网络问题。

你的代码对我有用(通过一些小修复来修复丢失的循环变量i):

public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack", "true");
String[] uat_targetUrls = { "https://www.google.com" };
String[] uat_targetResponse = new String[uat_targetUrls.length];

HttpURLConnection httpUrlConn;
httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[0])
.openConnection();

httpUrlConn.setRequestMethod("GET");

httpUrlConn.setConnectTimeout(30000);
httpUrlConn.setReadTimeout(30000);

if (httpUrlConn.getResponseCode() == 200)
uat_targetResponse[0] = "UP";
else
uat_targetResponse[0] = "DOWN";


System.out.println(uat_targetResponse[0]);
}

输出:向上

关于java.net.UnknownHostException :www. google.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825442/

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