需要有关奇怪的 java.net.HttpURLConnection 行为的帮助

发布于 2024-08-12 08:45:08 字数 818 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

耶耶耶 2024-08-19 08:45:08

该网站根据用户代理重定向您。在打开连接之前添加此内容,

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.15) Gecko/2009101600 Firefox/3.0.15");

The site redirects you based on user-agent. Add this before you open the connection,

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.15) Gecko/2009101600 Firefox/3.0.15");
泪痕残 2024-08-19 08:45:08

看起来服务器将您的请求解释为来自移动设备,可能基于 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.

欢烬 2024-08-19 08:45:08

为了获得更大的灵活性,您可以使用 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.

水晶透心 2024-08-19 08:45:08

虽然我无法帮助您解决您的具体问题,但我会这样做:

  • 下载 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)

攒眉千度 2024-08-19 08:45:08

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文