I'm looking for some help on how to download an image file from a password protected folder to my Android App. The code I have is using URLConnection along with getInputStream/BufferedInputStream but I don't see how to get the username/password authentication in there. I see that HttpClient has UsernamePasswordCredentials -- but I don't know how to download a file using HttpClient so that isn't helping me much.
Here's the code I found so far, how would I download a file using this?
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
HttpGet httpget = new HttpGet("https://localhost/protected");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
Or, I have this code for downloading file -- how would I add credentials to this:
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
Thanks!
EDIT: well, not getting much help here. I found this answer which I'm going to try and rework for my purposes: Download a file with DefaultHTTPClient and preemptive authentication