26

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!

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Adao
  • 261
  • 1
  • 3
  • 3
  • 1
    possible duplicate of [Why do I get a 403 error when I try open a URL](http://stackoverflow.com/questions/16826345/why-do-i-get-a-403-error-when-i-try-open-a-url) – Krease Aug 28 '14 at 17:03
  • @Krease While this isn't really a duplicate, your link was the solution I was looking for. Thanks! – SMBiggs May 15 '19 at 02:38

5 Answers5

67

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

Montezuma
  • 797
  • 6
  • 9
29

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.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
Saratha
  • 323
  • 3
  • 4
  • 3
    Casting to `HttpURLConnection` will not change anything here, the object returned by `url.openConnection()` is the same, whether you cast it or not. – Thomas Jul 16 '14 at 09:47
  • 6
    **What?** _"Normal java using urlConnection wont be accepted to access the internet"_ This is incorrect and misleading. Casting does not change what the underlying object is, and the OP is already setting the User-Agent request property... How does this have so many upvotes? – Krease Aug 28 '14 at 21:56
  • 2
    The reason why it has so many upvotes is because it actually solve the problem if the url connection is not adding the request property, and like me, people search in google and try this and it works, but it would work too if you only set the `URLConnection.addRequestProperty("User-Agent", "Mozilla/4.76")` – centenond Mar 28 '17 at 00:09
  • https://www.cftc.gov issued this error (403) for the first time yesterday. To resolve the problem, it was sufficient to add just conn.addRequestProperty("User-Agent", "Chrome"); where conn denotes simple URLConnection. Thanks for the help! – alrts Aug 21 '18 at 08:25
2

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.

Alexander Sagen
  • 4,028
  • 1
  • 18
  • 15
2

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.

arthur
  • 3,245
  • 4
  • 25
  • 34
0

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.

runlevel0
  • 2,715
  • 2
  • 24
  • 31