0

I'm trying to download an image using Java, however a 403 error code is returned:

java.io.IOException: Server returned HTTP response code: 403 for URL: https://someurl.com

I've used the command curl with the same URL and it returns OK

curl -I https://someurl.com

HTTP/1.1 200 OK Date: Tue, 11 Apr 2017 18:36:49 GMT Content-Type: image/png Content-Length: 4233029 Connection: keep-alive

This is the code I'm using to download the images:

private static boolean downloadImage(String imageURL, String path)
{
    InputStream in;

    try 
    {            
        URL url = new URL(imageURL);

        in = new BufferedInputStream(url.openStream());

        Files.copy(in, Paths.get(path), StandardCopyOption.REPLACE_EXISTING);

    } catch (MalformedURLException ex) {
        LOG.error(ex.toString());

        return false;

    } catch (IOException ex) {
        LOG.error(ex.toString());

        return false;
    }

    return true;
}

Any idea of why it fails with Java but it does work with the curl command?

Thanks in advance,

Dani M
  • 1,173
  • 1
  • 15
  • 43
  • Possible duplicate of [java.io.IOException: Server returned HTTP response code: 403 for URL](http://stackoverflow.com/questions/3869372/java-io-ioexception-server-returned-http-response-code-403-for-url?rq=1) – Ivan Pronin Apr 11 '17 at 18:43
  • if possible paste your url, maybe need set some header, for instance, `User-Agent`. – neuo Apr 12 '17 at 06:28
  • @neuo How do I set the User-Agent with the method I'm using? Do I have to change the way I download the images? – Dani M Apr 12 '17 at 06:38
  • @cuoka You can get image with curl, which confirm the url is accessible. You can read the link appeared in first comment, it may be helpful. – neuo Apr 12 '17 at 06:45

0 Answers0