Java 不支持HTTP版本:505
我已经编写了selenium/Java脚本来查找网站上的损坏图像。在34个映像中,有2个映像抛出505错误代码(不支持HTTP版本) 这是我的密码Java 不支持HTTP版本:505,java,selenium,brokenimage,Java,Selenium,Brokenimage,我已经编写了selenium/Java脚本来查找网站上的损坏图像。在34个映像中,有2个映像抛出505错误代码(不支持HTTP版本) 这是我的密码 HttpURLConnection huc = null; int respCode = 200; huc = (HttpURLConnection) (new URL(url).openConnection()); huc.setRequestMethod("HEAD"); huc.setConnectTi
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");
}
问题在于URL,它包含空格。您可以将Java URL编码器用于通用解决方案: 在您的情况下,只需按如下方式替换%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
问题在于URL,它包含空格。您可以将Java URL编码器用于通用解决方案: 在您的情况下,只需按如下方式替换%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
如果您使用Selenium和Java,您应该将库“免费”作为Selenium的一部分 因此,您可以将图像检查逻辑修改为:
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客户端将自动处理由于特殊原因导致的当前请求失败
您可能还想将它们添加到您的请求中,因为您的端点可能需要基于cookie的身份验证如果您使用Selenium和Java,您应该“免费”将库作为Selenium的一部分 因此,您可以将图像检查逻辑修改为:
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客户端将自动处理由于特殊原因导致的当前请求失败
您可能还希望将它们添加到您的请求中,因为您的端点可能需要基于cookie的身份验证。如果两个给出错误,四个工作,我怀疑这是代码问题。@Elliott Frisch-会有什么问题?请帮我翻译一下。如果两个给出错误,四个工作,我怀疑这是代码问题。@Elliott Frisch-会有什么问题?请帮帮我