Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - how android downloadManager do http basic authentication

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()));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...