views:

77

answers:

2

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!

+1  A: 

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
doesn't work, but thanks anyway.
Adao
A: 

Check the following link for explanation of 403 status code and possible fixes: http://www.checkupdown.com/status/E403.html

Suresh Kumar