Search code examples
javahttp-status-code-404htmlunit

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.


Solution

  • 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.