Re: The simplest way to download a file from http resource that need
authentication
Andrea Francia wrote:
Lew wrote:
Andrea Francia wrote:
Authenticator.setDefault() method which is a static method and therefore
not usable in a threaded enviroment.
Static methods can be used in a multi-threaded program.
There is a race conditions. Here the example:
We have two thread: t1 and t2 that executes the following code
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
password.toCharArray());
}});
URLConnection con = url.openConnection();
Authenticator.setDefault is designed for proxy servers that
requires authentication and in that context all requests
need the same authenticator.
I think you will need to set HTTP headers manually.
Something like:
con.setRequestProperty ("Authorization", "Basic " +
basicauth("user","pass"));
where:
public static String basicauth(String un, String pw) throws
MessagingException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write((un + ":" + pw).getBytes());
b64os.close();
return new String(baos.toByteArray());
}
Arne