如何Android的下载管理器做HTTP基本身份验证 [英] how android downloadManager do http basic authentication

查看:146
本文介绍了如何Android的下载管理器做HTTP基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Android的下载管理器来下载文件;但是URL是HTTP基本身份验证。而且我可以在应用程序的用户名和密码。我应该怎么做,从我的主机下载文件?

 下载管理器下载管理器=(下载管理器)appContext.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request请求=新DownloadManager.Request(Uri.parse(URL));
downloadManager.enqueue(要求);
 

这是我的code。我想通过URL,下载文件;但它需要HTTP基本身份验证。我想知道如何添加像这样的认证:

  httpClient.getState()setCredentials方法(新AuthScope(HOST,80),新UsernamePasswordCredentials(user.getEmail(),user.getPassword()))。
 

解决方案

您可以使用<一个href="http://developer.android.com/reference/android/app/DownloadManager.Request.html#addRequestHeader(java.lang.String,%20java.lang.String)"相对=nofollow> DownloadManager.Request.addRequestHeader(字符串头,字符串值)在要求对象手动​​添加HTTP的批准<方法/ STRONG>头。

您可以阅读更多有关维基百科标题的格式,但基本上你只取用户名和密码,他们一起用冒号:字符,然后使用Base64恩code结果

一旦你有你的连接codeD凭据,将它们添加到与DownloadManager.Request对象:

  request.addRequestHeader(授权,基本+ EN codedCredentials);
 

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

解决方案

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

这篇关于如何Android的下载管理器做HTTP基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆