java.io.IOException: Server returned HTTP response code: 403 for URL
Asked Answered
C

5

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!

Calends answered 6/10, 2010 at 3:45 Comment(2)
possible duplicate of Why do I get a 403 error when I try open a URLZeniazenith
@Zeniazenith While this isn't really a duplicate, your link was the solution I was looking for. Thanks!Scone
T
68

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

Tournai answered 15/3, 2016 at 16:6 Comment(5)
Thanks it helps me!Forethoughtful
Thank you, this should be the accepted answer! Worked for me!Nutmeg
This is not woking for the production serverBraunite
Thank you very much! You save my implementation!Intercept
Thanks a bunch. This fixed the problem!Longdistance
A
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.

Aggrieved answered 5/3, 2011 at 6:47 Comment(4)
Casting to HttpURLConnection will not change anything here, the object returned by url.openConnection() is the same, whether you cast it or not.Pennoncel
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?Zeniazenith
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")Zoraidazorana
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!Aedile
T
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.

Tiger answered 6/10, 2010 at 3:50 Comment(0)
D
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.

Desdamonna answered 23/7, 2011 at 19:30 Comment(0)
A
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.

Alric answered 24/4, 2014 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.