gpt4 book ai didi

java - 如何刷新网站直到服务器有响应?在Java中?

转载 作者:行者123 更新时间:2023-12-02 11:27:30 25 4
gpt4 key购买 nike

我尝试访问繁忙的网站,但总是得到繁忙的服务器结果(错误 500)。

我想使用java代码循环该网站,直到我可以进入该网站,但我只知道如何获取响应代码,不知道如何在浏览中打开它。

如何在浏览中打开HttpURLConnection连接中的网站或者是否有其他方式打开或循环网站直到没有错误?非常感谢!!!

import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;

public class test{
public static String URL = "http://www.example.com/";
public static void main (String args[]) throws Exception{

int i =0;
while(i==0){
URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
if (code != 500){
//how to open the no error website.
}
System.out.println(code);
}
}

}

最佳答案

如果您想在收到 200 HTTP 状态后打开网站,您可以在 if ... else 语句中执行如下操作:

   if (code == 200){
//how to open the no error website.
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(url.toURI()); //referring to the url connection already created
System.exit(0);
}
}

关于java - 如何刷新网站直到服务器有响应?在Java中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503822/

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