[英]ImageIO.read() returns 403 error
I have the following code: 我有以下代码:
public BufferedImage urlToImage(String imageUrl) throws MalformedURLException, IOException {
URL url = new URL(imageUrl);
BufferedImage image = ImageIO.read(url);
return image;
}
That is supposed to return an image from a given URL. 这应该是从给定的URL返回一个图像。
I tested with these two randomly chosen URLs: 我测试了这两个随机选择的URL:
The first one works fine, but the second gives a 403 error: 第一个工作正常,但第二个给出403错误:
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.URL.openStream(URL.java:1010)
at javax.imageio.ImageIO.read(ImageIO.java:1367)
What could be the cause of the error ? 可能是错误的原因是什么? Thanks. 谢谢。
The ImageIO.read(URL)
method opens a URL connection with pretty much all default settings, including the User-Agent
property (which will be set to the JVM version you are running on). ImageIO.read(URL)
方法打开一个URL连接,其中包含几乎所有默认设置,包括User-Agent
属性(将设置为您运行的JVM版本)。 Apparently, the site you listed expects a more 'standard' UA. 显然,您列出的网站需要更“标准”的UA。 Testing with a straight telnet connection: 使用直接telnet连接进行测试:
Request sent by ImageIO.read(url)
: ImageIO.read(url)
发送的请求:
GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP/1.1 GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP / 1.1
User-Agent: Java/1.7.0_17 用户代理:Java / 1.7.0_17
Host: www.earthtimes.org 主持人:www.earthtimes.org
Accept: text/html, image/gif, image/jpeg, *; 接受:text / html,image / gif,image / jpeg,*; q=.2, / ; q = .2, / ; q=.2 Q = 0.2
Connection: keep-alive 连接:保持活力
Response code is 404 (for me at least), with a default text/html
page being returned. 响应代码是404(至少对我而言),返回默认的text/html
页面。
Request sent by 'standard' browser: “标准”浏览器发送的请求:
GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP/1.1 GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP / 1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31 用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_7_5)AppleWebKit / 537.31(KHTML,类似Gecko)Chrome / 26.0.1410.65 Safari / 537.31
Host: www.earthtimes.org 主持人:www.earthtimes.org
Accept: text/html, image/gif, image/jpeg, *; 接受:text / html,image / gif,image / jpeg,*; q=.2, / ; q = .2, / ; q=.2 Q = 0.2
Connection: keep-alive 连接:保持活力
Response code is 200, with the image data. 响应代码为200,带有图像数据。
The following simple fix lengthens your code, but gets around the problem, by setting a more 'standard' UA: 以下简单修复程序会延长您的代码,但通过设置更“标准”的UA来解决问题:
final String urlStr = "http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg";
final URL url = new URL(urlStr);
final HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
final BufferedImage image = ImageIO.read(connection.getInputStream());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.