HtmlUnit - 服务器 (HTTP) 状态代码在服务器关闭后没有改变

HtmlUnit - Server (HTTP) status code is not changing after Server is turned off

通过使用 HtmlUnit 浏览器,我获得了 HTTP 标准响应代码。根据响应代码,我们正在决定服务器是否 运行 或不控制另一个线程。

我们在这里面临的问题是,如果我们停止服务器,我们仍然会得到 200 (运行) 响应代码。

站点的索引文件是一个普通的 HTML 文件,所以我们通过清除 Cookie 和缓存进行了尝试。但是即使我们关闭了服务器,我们仍然得到 200 的响应代码。请指导找到一种方法来跟踪 运行 服务器何时变为 off/not 响应(HttpHostConnectException)

HtmlUnit代码:

while(alive)
{
    try
    {
    c.clear();
    cm.clearCookies();
    SST = client.getPage("http://**ip**/DDT/").getWebResponse().getStatusCode();
    }
    catch(HttpHostConnectException he)
    {
        isServerUP = false;
    }
    catch(FailingHttpStatusCodeException fhe)
    {
        isServerUP = false;
    }
    System.out.println(SST);
    if(SST == 200)
    {
        isServerUP = true;
    }
    else
    {
        isServerUP = false;
    }
}

我无法重现,可能是 WebClient 配置不同。

使用最新版本,以下成功处理 stopping/starting 服务器:

try (WebClient webClient = new WebClient()) {
    while (true) {
        try {
            int status = webClient.getPage("http://localhost/test.html").getWebResponse().getStatusCode();
            System.out.println(new Date() + " " + status);
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        Thread.sleep(1000);
    }
}