需要有关怪异的java.net.HttpURLConnection行为的帮助 [英] need help with bizarre java.net.HttpURLConnection behavior

查看:66
本文介绍了需要有关怪异的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.

我猜测是由于某些标题错误导致的,但我还没有最清楚的主意,主机期望将我重定向到与重定向浏览器相同的位置(并卷曲)和wget以及其他所有我想指出的内容.

我正准备向自己开枪,因此,如果您帮助我,您将防止我6岁的女儿失去父亲.

解决方案

该站点根据用户代理重定向您.在打开连接之前添加它,

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");

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.

解决方案

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");

这篇关于需要有关怪异的java.net.HttpURLConnection行为的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆