How android downloadManager does http basic authentication

I want to use android downloadManager to download files; But the url is in basic http authentication. And I can get the username and password in the application. What to 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 upload a file via "url"; But this requires HTTP authentication. I want to know how to add authentication as follows:

 httpClient.getState().setCredentials(new AuthScope(HOST, 80), new UsernamePasswordCredentials(user.getEmail(), user.getPassword())); 
+7
java android
source share
1 answer

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

You can read more about the format of this header in Wikipedia , but basically you just take the username and password, attach them to the colon symbol: ', then base64 encodes the result.

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

 request.addRequestHeader("Authorization", "Basic " + encodedCredentials); 
+13
source

All Articles