使用Java/Selenium检测网站图片时遇HTTP 505错误的技术问询
Let’s break down why those two image links are throwing 505 errors and get them sorted out. First, a quick note: HTTP 505 technically means the server doesn’t support the HTTP version used in your request, but in your case, this is almost certainly a side effect of malformed URLs rather than an actual version mismatch.
What’s Probably Causing the 505s?
Looking at your broken links, I spot unencoded spaces (like the gap between 1 and Desktop(new) in the file path). While some of your other links with spaces work, this inconsistency is common with services like Adobe Scene7 (which hosts these images)—the server might tolerate unencoded spaces in some parameter combinations but not others. When it can’t parse the messy URL correctly, it throws a 505 error as a fallback.
Step-by-Step Fixes
Properly URL-Encode Spaces and Special Characters
The first fix to try is encoding the URL before sending the request. Spaces in URLs must be replaced with%20(universally safe) instead of being left raw. Here’s how to adjust your code to do this correctly:HttpURLConnection huc = null; int respCode = 200; // Split and encode the URL to handle spaces/special characters URL originalUrl = new URL(url); String protocol = originalUrl.getProtocol(); String host = originalUrl.getHost(); // Encode the path, replacing + (URLEncoder's default space) with %20 String encodedPath = URLEncoder.encode(originalUrl.getPath(), "UTF-8").replace("+", "%20"); String query = originalUrl.getQuery(); String encodedQuery = ""; if (query != null) { encodedQuery = "?" + URLEncoder.encode(query, "UTF-8").replace("+", "%20"); } String encodedUrl = protocol + "://" + host + encodedPath + encodedQuery; huc = (HttpURLConnection) (new URL(encodedUrl).openConnection()); huc.setRequestMethod("HEAD"); huc.setConnectTimeout(2000); // Add a browser-like User-Agent to avoid being flagged as a bot huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"); huc.connect(); respCode = huc.getResponseCode(); if (respCode >= 400) { System.out.println(url + " is broken with error code:" + respCode); } else { System.out.println(url + " is good"); }Test the Links Directly in a Browser
Copy the broken links into a web browser and see if they load. If they don’t, the issue isn’t with your script—it’s with the image configuration on Scene7. For example, the$callToActionCard_tablet$or$callToActionCard_desktop$presets might be missing or misconfigured for those specific images. You’ll need to reach out to your team that manages Scene7 assets to fix the preset or image mapping.Explicitly Set HTTP Version (Optional)
While less likely, you can force the request to use HTTP/1.1 (the most widely supported version) to rule out any version-related quirks:huc.setRequestProperty("HTTP-Version", "HTTP/1.1");
Quick Validation
After updating your code to encode URLs, run your script again. If the 505 errors disappear, the unencoded spaces were the root cause. If not, the problem lives on the Scene7 side, and you’ll need to validate the image presets with your content team.
内容的提问来源于stack exchange,提问作者Chris

