我编写了 selenium/Java 脚本来查找网站上损坏的图像。在 34 个图像中,有 2 个图像抛出 505 错误代码(不支持 HTTP 版本)。
这是我的代码
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
最佳答案
问题出在 URL 上,它包含空格。您可以使用 Java URL 编码器作为通用解决方案:Java URL encoding of query string parameters
在您的情况下,只需替换 %20 中的空格,如下所示:
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");
}
输出:
https://s7d4.scene7.com/is/image/DuPontCorteva/IMG-Soja-CampoDeSojaConFocoYDesenfoque-0_71-1%20Desktop(new)?$callToActionCard_tablet$ is a good
关于java - 不支持 HTTP 版本 :505,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56203970/