Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 699
EK_AllDay Avatar asked Dec 08 '22 23:12

EK_AllDay


1 Answers

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.

like image 189
plasma147 Avatar answered Dec 28 '22 06:12

plasma147