java.io.IOException: Server returned HTTP response

2019-01-21 20:58发布

I want to download the mp3 file from url : "http://upload13.music.qzone.soso.com/30671794.mp3", i always got java.io.IOException: Server returned HTTP response code: 403 for URL. But it's ok when open the url using browser. Below is part of my code:

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
    URL url = new URL(link);

    URLConnection urlConn = url.openConnection();
    urlConn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

    String contentType = urlConn.getContentType();

    System.out.println("contentType:" + contentType);

    InputStream is = urlConn.getInputStream();
    bis = new BufferedInputStream(is, 4 * 1024);
    bos = new BufferedOutputStream(new FileOutputStream(
    fileName.toString()));​

Anyone could help me? Thanks in advance!

5条回答
我想做一个坏孩纸
2楼-- · 2019-01-21 21:02

I would also check if the server were the resource is located has an ACL or similar in place, we just resolved a "java.io.IOException: 403" issue this way.

It happens that 403 errors are very generic and you cannot really be sure of the source as it can be just anything.

查看更多
看我几分像从前
3楼-- · 2019-01-21 21:06

When I access the URL with my browser I also get 403. Perhaps you're logged in to the site with your browser?

If that's the case you need to duplicate the cookie from your browser and send it along, perhaps even do more to replicate your browser's signature if the site does any extra checks.

You can set the cookie by adding:

urlConn.setRequestProperty("Cookie", "foo=bar"); 

Where foo=bar is the key-value pair you'll find when you locate the site's cookie in your browser.

查看更多
闹够了就滚
4楼-- · 2019-01-21 21:09

The problem is given by the Status code. 403 means actually "Forbidden" and implies The request was denied for a reason the server does not want to (or has no means to) indicate to the client.

the problem lies at the server-side.

查看更多
三岁会撩人
5楼-- · 2019-01-21 21:11

Instead of using URLConnection in java, if you use HttpURLConnection you should beable to access the requested web page from java. Try the following code:

 HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
 httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); 

Normal java using urlConnection wont be accepted to access the internet. To access the browser it will need to perform a search without theexception HTTP response code : 403 for URL


EDIT (@Mordechai): No need to do the casting, just add the user agent.

查看更多
干净又极端
6楼-- · 2019-01-21 21:15

You can also use

System.setProperty("http.agent", "Chrome");

it worked for me.

//Update

Explanation

Because HttpURLConnection reads the property "http.agent" if set. You can read it here: https://www.innovation.ch/java/HTTPClient/advanced_info.html

Or you can look it up in the source code of the HttpURLConnection Class:

String agent = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("http.agent"));

查看更多
登录 后发表回答