0

I need to implement the functionality to download multiple files from an authenticated server. But I'm receiving the following result, even though accessing via browser makes the download starts normally (if authenticated).

'Server returned HTTP response code: 500'

File size should be something between 7.10 KB and 4.8 GB

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

public class Example {

    private static final String URL = "totally_a_valid_url";
    private static final String PATH = "D:\\___Automation\\_TEST";

    public static void main(String[] args) throws IOException {

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL(URL);
        File destination = new File(PATH);
        FileUtils.copyURLToFile(url, destination);

    }

    public static class MyAuthenticator extends Authenticator {

        String uName = "not_my_real_usr";
        String uPass = "not_my_password";

        PasswordAuthentication authentication;

        MyAuthenticator() {
            authentication = new PasswordAuthentication(uName, uPass.toCharArray());
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }

    }

}
Gabriel Brito
  • 1,003
  • 2
  • 16
  • 26
  • HTTP 500 means that the server had a problem. Look into the server's log what exactly happened. – Lothar May 18 '18 at 18:34
  • Unfortunately i don't have access to server's log. Using the same url I was able to download the file normally with chrome. I thought something went wrong with how the request was made. – Gabriel Brito May 18 '18 at 18:40
  • 2
    I bet it is not a public URL and your chrome had to login first etc. If yes, you would have cookies, session, headers which you need to investigate and replicate in you code client request. Most likely the request you compose is incomplete. – Oleg Sklyar May 18 '18 at 19:10
  • A note that a lot of automation requires so many session, cookie, and headers specifications that it's more trouble than it's worth to reverse engineer it. That's why people use Selenium to power to automate the actions you want to do directly on the browser. – ifly6 May 18 '18 at 20:14
  • I've already done with selenium but from time to time I'm asked to make it on headless mode to run it on a server but I'm also having problem making it work on headless mode =/ – Gabriel Brito May 18 '18 at 21:15
  • @OlegSklyar as you sad, it wasn't a public URL and chrome was making other requests to fetch the real download link so it could start the download. – Gabriel Brito May 24 '18 at 17:44

0 Answers0