EK_AllDay
EK_AllDay

Reputation: 1115

HtmlUnit webpage status code

I am trying to get the web status for a given page. However when its a 404 error, the page does not return the status code, rather it throws and error.

int status= webClient.getPage("website").getWebResponse().getStatusCode();
System.out.println( status);

Any Ideas?

I am looking to see when sites time out, however for testing purposes I malformed the url of the desired website to see if I can even see a 404.

Upvotes: 4

Views: 5888

Answers (2)

James Crocker
James Crocker

Reputation: 1

Alternatively, you can continue to allow the FailingHttpStatusCodeException to be thrown (true). Then within the catch clause get the error status code.

...
int status = 0;
Object page = null;
try {
     page = webClient.getPage(webRequest);
     webClient.close();
     if (page instanceof UnexpectedPage) {
         status = ((UnexpectedPage) page).getWebResponse().getStatusCode();
     } else if (page instanceof HtmlPage) {
         status = ((HtmlPage) page).getWebResponse().getStatusCode();
     }
     // do something else ...
} catch (FailingHttpStatusCodeException | IOException e) {
     if (e instanceof FailingHttpStatusCodeException) {
          status = ((FailingHttpStatusCodeException) e).getStatusCode();
     }
     // do something else ...
}

Upvotes: 0

plasma147
plasma147

Reputation: 2211

According to this

You can do this:

webclient.setThrowExceptionOnFailingStatusCode(False)

****EDIT ***

This does print out your status code:

 WebClient webClient = new WebClient();
 webClient.setThrowExceptionOnFailingStatusCode(false);
 int status = webClient.getPage("http://google.co.uk/ffffff").getWebResponse()
            .getStatusCode();
 System.out.println(status);

Prints out 404 - your status code.

Upvotes: 14

Related Questions