Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how android downloadManager do http basic authentication

Tags:

java

android

I want to use android downloadManager to download files; But the url is in http basic authentication. And I can get the user name and password in the application. What should I do to download files from my host?

DownloadManager downloadManager = (DownloadManager) appContext.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
downloadManager.enqueue(request);

This is my code. I want to download file via "url"; But it need http basic authentication. I want to know how to add authentication like this:

httpClient.getState().setCredentials(new AuthScope(HOST, 80), new UsernamePasswordCredentials(user.getEmail(), user.getPassword()));
like image 605
xierui Avatar asked Sep 04 '14 15:09

xierui


1 Answers

You can use the DownloadManager.Request.addRequestHeader(String header, String value) method on your request object to manually add the HTTP Authorization header.

You can read more about the format of this header on Wikipedia, but basically you just take the username and password, join them with a colon ':' character, then base64-encode the result.

Once you have your encoded credentials, add them to the DownloadManager.Request object with:

request.addRequestHeader("Authorization", "Basic " + encodedCredentials);
like image 69
Alex MDC Avatar answered Oct 15 '22 21:10

Alex MDC