How to download a file from a protected web folder in Android App?
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 = http开发者_StackOverflow中文版client.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
Well, this is the code I've come up with, seems to work. I'm posting it since I had so much trouble finding code like this anywhere on the web. I welcome suggestions on how to improve it :)
public void downloadHTTPC(Activity act, String imageURL, String fileName) {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
String pathDir = act.getExternalFilesDir(null).toString() + "/" + fileName;
File file = new File(pathDir);
long startTime = System.currentTimeMillis();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials("user", "password"));
HttpGet httpget = new HttpGet(IMGURL + imageURL);
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());
InputStream is = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a Stream. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
}
//EntityUtils.consume(entity);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
精彩评论