需要有关奇怪的 java.net.HttpURLConnection 行为的帮助
我正在尝试使用 HttpURLConnection 下载 jpg,但遇到了一个非常奇怪的错误。
这是网址: http://www.vh1. com/sitewide/promoimages/shows/m/my_antonio/video/supertrailer/seg_1/320x240.jpg
如果您在浏览器中打开它,您将看到该图像。
但是,当我使用 HttpURLConnection 时,我没有得到图像...... 我得到的是 301,很奇怪的是,它重定向到 http://wap.vh1.com
所以
con.setInstanceFollowRedirects(true);
//additional stream code here to go and get the stuff found in con
继续继续从 wap.vh1.com 下载文本,而不是您在浏览器中看到的 jpg。
我猜测是有一些奇怪的标题导致了这种情况,但我完全不知道主机期望看到什么,以便将我重定向到与重定向浏览器相同的位置(以及curl和wget和我能想到的其他一切都可以指出这一点)。
我正准备开枪自杀,所以,如果你帮助我,你就能阻止我 6 岁的女儿失去父亲。
I am attempting to download a jpg using HttpURLConnection and am encountering a very peculiar bug.
Here's the url:
http://www.vh1.com/sitewide/promoimages/shows/m/my_antonio/video/supertrailer/seg_1/320x240.jpg
if you open it in a browser you will see the image.
However, when I use HttpURLConnection I don't get the image...
What I get is a 301 which, quite strangely, redirects to http://wap.vh1.com
so
con.setInstanceFollowRedirects(true);
//additional stream code here to go and get the stuff found in con
proceeds to go ahead and download the text from wap.vh1.com, rather than the jpg that you see in the browser.
I'm guessing that there is some header wackiness that's causing this, but I haven't the faintest idea what the host is expecting to see in order to redirect me to the same place as where it's redirecting the browser (and curl and wget and everything else I can think to point at it).
I'm just about ready to shoot myself, so, if you help me you will be preventing my 6 year old daughter from going fatherless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该网站根据用户代理重定向您。在打开连接之前添加此内容,
The site redirects you based on user-agent. Add this before you open the connection,
看起来服务器将您的请求解释为来自移动设备,可能基于 User-Agent 标头。这就是您重定向到移动网站的原因。尝试显式设置用户代理。
It seems like the server interprets your request as coming from a mobile device, possibly based on the User-Agent header. That's why your redirected to the mobile site. Try setting the User-Agent explicitly.
为了获得更大的灵活性,您可以使用 http 公共库,它对 wire< /em> 通过 log4j ...
此外,可以轻松设置用户代理和更多请求参数。
有关详细信息,请参阅他们的教程。
For more flexibility you can utilize the http commons libraries, which have great debugging support for the wire through log4j ...
Also, user agents and more request parameters can be set easily.
For more information, see their tutorial.
虽然我无法帮助您解决您的具体问题,但我会这样做:
下载 wireshark ,嗅探您的java应用程序发送的HTTP请求
复制/粘贴请求,然后使用 telnet(或诸如 WFetch)
摆弄请求标头并查看行为是否发生变化。
(我怀疑该网站根据用户代理标头或类似内容筛选请求)
While I can't help you with your specific problem, here's what I would do:
Download wireshark, sniff the HTTP request sent by your java application
Copy/Paste the request, and run it with telnet (or a tool such as WFetch)
Fiddle with the request headers and see if behavior changes.
(I'd suspect the site screens the request based on the User agent header or something similar)
java.net 包不支持许多开箱即用的所需功能(例如自动保存和发送 cookie)。使用 Apache 的 httpClient 代替
The java.net package doesn't support lot of the needed features out of the box (like automatically saving and sending cookies). Use Apache's httpClient instead