Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 746
Adao Avatar asked Oct 06 '10 03:10

Adao


People also ask

Could not check for updates server returned HTTP response code 403 for URL?

The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it... If authentication credentials were provided in the request, the server considers them insufficient to grant access. The 403 response belongs to the 4xx range of HTTP responses: Client errors.

What is a 403 error code?

The HTTP 403 Forbidden response status code indicates that the server understands the request but refuses to authorize it. This status is similar to 401 , but for the 403 Forbidden status code re-authenticating makes no difference.


1 Answers

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

like image 179
Montezuma Avatar answered Sep 28 '22 07:09

Montezuma