Code that should catch page loading status

public static int getResponseCode(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection huc = (HttpURLConnection)url.openConnection(); huc.setRequestMethod("GET"); huc.connect(); return huc.getResponseCode();} private static int statusCode; 

The cycle in which I check the status of the pages

 for(int i = 0; i < url.length; i++) {try {driver.get(url[i]); driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); statusCode = getResponseCode(url[i]); if(statusCode >= 300){System.out.println("HTTP code "+statusCode+" on "+url[i]);} else {System.out.println("Code is "+statusCode);}} catch (Exception e){System.out.println("Exception. Probably 50X");}} 

I use catch (Exception e) because I can’t catch 500e loading statuses, can I do something wrong?

    1 answer 1

    It seems like they abandoned such gadgets.

    But the status can still be read by sending your request to the server using url using HtmlUnit:

     try (WebClient webClient = new WebClient()) { WebClientOptions options = webClient.getOptions(); options.setThrowExceptionOnFailingStatusCode(false); WebResponse webResponse = webClient.getPage("https://url.com").getWebResponse(); int code = webResponse.getStatusCode(); } catch (FailingHttpStatusCodeException | IOException e) { e.printStackTrace(); } 
    • In my test, I go through the internal pages after logging in, does this command require additional authorization parameters for this? - Dmitry Feniks
    • Yes, you will then have to first post a login request to create a session for the HY client, and then you can immediately punch what is there for the statuscode. If the authorization is through cookies, then maybe it will be possible to share something like a cookie ... - Webaib
    • Something to me this team throws a huge pile of garbage into the log. I do not quite understand what kind of information she throws out (in particular, a piece of page code) - Dmitry Feniks
    • @DmitryFeniks This is just an error stream, code corrected. - Webaib
    • How can I pass authorization parameters before checking download status? - Dmitry Feniks