By using HtmlUnit browser, I am getting the HTTP standard response code. Based on response code, we are deciding whether the server is running or not to control another thread.
Problem what we are facing here is, if we stop the Server, still we get 200 (running) response code.
Index file of the site is a plain HTML file, so we tried by clearing Cookie and Cache. But still we are getting the response code as 200 even after we switched off the server. Please guide to find a way to track when the running server become off/not responding (HttpHostConnectException)
HtmlUnit code:
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;
}
}
I am unable to reproduce, possibly WebClient is differently configured.
With latest version, the below successfully handles stopping/starting the server:
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);
}
}