0

I have written selenium/Java script to find out broken images on a website. Out of 34 images, 2 images are throwing 505 error code(HTTP Version not supported).

Here is my code

    HttpURLConnection huc = null;
    int respCode = 200;
    huc = (HttpURLConnection) (new URL(url).openConnection());
    huc.setRequestMethod("HEAD");

    huc.setConnectTimeout(2000);
    huc.connect();
    respCode = huc.getResponseCode();

    if (respCode >= 400) {
        System.out.println(url + " is a broken with error code:" + respCode);
    } else {
        System.out.println(url + " is a good");
    }

505 Errors:
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Soja-CampoDeSojaConFocoYDesenfoque-0_71-1 Desktop(new)?$callToActionCard_tablet$ is a broken with error code:505

https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Cenital-VistaCenitalDeCampo-0_71-1 Desktop(new)?$callToActionCard_desktop$ is a broken with error code:505


Few successful responses:
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Campo-PersonasCaminandoEnElCampoConAtardecer-0_71-1 Desktop(new)?$callToActionCard_desktop$ is a good
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Campo-PersonasCaminandoEnElCampoConAtardecer-0_71-1 Desktop(new)?$callToActionCard_tablet$ is a good
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Campo-PersonasCaminandoEnElCampoConAtardecer-0_71-1 Desktop(new)?$callToActionCard_mobile$ is a good
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Soja-CampoDeSojaConFocoYDesenfoque-0_71-1 Desktop(new)?$callToActionCard_desktop$ is a good 
Chris
  • 236
  • 4
  • 14

2 Answers2

1

Issue is with the URL, it contains spaces. You can use Java URL encoder for a generic solution: Java URL encoding of query string parameters

In your case, just replace your space in %20 as follows:

        String url = "https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Soja-CampoDeSojaConFocoYDesenfoque-0_71-1%20Desktop(new)?$callToActionCard_tablet$";
    HttpURLConnection huc = null;
    int respCode = 200;
    huc = (HttpURLConnection) (new URL(url).openConnection());
    huc.setRequestMethod("HEAD");

    huc.setConnectTimeout(2000);
    huc.connect();
    respCode = huc.getResponseCode();

    if (respCode >= 400) {
        System.out.println(url + " is a broken with error code:" + respCode);
    } else {
        System.out.println(url + " is a good");
    }

output:

https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Soja-CampoDeSojaConFocoYDesenfoque-0_71-1%20Desktop(new)?$callToActionCard_tablet$ is a good
Adi Ohana
  • 927
  • 2
  • 13
  • 18
1

Given you use Selenium and Java you should have OkHttp library "for free" as a part of Selenium's transitive dependencies.

Therefore you can amend your image checking logic to look like:

OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder().url(url).method("HEAD", null).build();
Response response = client.newCall(request).execute();
int respCode = response.code();

OkHttp client will automatically take care of the URL Encoding as your current request fails due to special characters which are not allowed in URLs

You might also want to obtain browser cookies and add them to your request as your endpoint might require cookie-based authentication

Dmitri T
  • 159,985
  • 5
  • 83
  • 133