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;
}
}
}