0

Below code downloads an image with specified url. But I am getting 403 error for some urls.

According to this link, I tried using setRequestProperty() but still my problem isn't solved. I couldn't figure out the mistake that I made or is there anything more that I should add to my code?

import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.net.HttpURLConnection;

class Crawler{

    public static void main(String args[]){

        String address = "http://szcdn1.raagalahari.com/dec2016/hd/anupama-parameswaran-premam-hd-photos/anupama-parameswaran-premam-hd-photos294.jpg";
        Connection connection1 = new Connection();
        connection1.connector(address);

    }   
}

class Connection{
    void connector(String s){
        try{    
            URL url = new URL(s);
            URLConnection uc = url.openConnection();
            HttpURLConnection http_connection = (HttpURLConnection) uc;
            http_connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36");

            http_connection.connect();
            ImageDownload downloader = new ImageDownload();
            downloader.download(url);   

        }catch(Exception e) {
            System.out.println(e);
        }       
    }
}

class ImageDownload{

    void download(URL u){
        try
        { 

            InputStream in = new BufferedInputStream(u.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;

            while (-1!=(n=in.read(buf))){
                out.write(buf, 0, n);
            }

            out.close();
            in.close();

            byte[] response = out.toByteArray();


            FileOutputStream fos = new FileOutputStream("C://3.jpg");
            fos.write(response);
            fos.close();
        } catch(IOException e){
             System.out.println(e);
        }
    }
}

Sorry in advance if the question is duplicate. Please help..

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93

2 Answers2

0

403 errors are almost always caused by issues where you're trying to access something that you don't have access to.

try to use this

http_connection.setRequestProperty("http-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
0

Your download() method opens its connection from scratch using URL.openStream(), using Java's default connection parameters. The http_connection that you prepared has no effect, because your code doesn't use it in the download() method that's doing the work.

So, instead of the URL, you should pass the http_connection into the download() method and use its getInputStream() method instead of URL.openStream(). Then you'll see effects of request properties.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
  • So I made these changes : 1. 'downloader.download(http_connection) ' 2.'void download(HttpURLConnection u)' 3. 'InputStream in = u.getInputStream();' But this gives me the same error : Server returned HTTP response code: 403 – Finding_me Jun 24 '17 at 19:32