当前位置: 代码迷 >> Android >> Android终端实现Http的摘要(Digest)认证解决思路
  详细解决方案

Android终端实现Http的摘要(Digest)认证解决思路

热度:35   发布时间:2016-05-01 22:08:34.0
Android终端实现Http的摘要(Digest)认证

请问, 如何在Android终端实现Http的摘要(Digest)认证, 到网上搜了下, 都说的很笼统, 有没找到相关的API. 请高人指教.

------解决方案--------------------
我搜了一下资料,我没有自己实践过,还需要你亲自验证一下才好

基本思路:

--- 使用HttpClient(DefaultHttpClient)类连接服务器并获取数据

DefualtHttpClient 类:创建client对象

excuete(HttpGet)方法:执行连接和获取,参数是一个HttpGet对象

HttpGet类:基于url创建HttpGet对象

HttpResponse类: execute的返回值

getEntiry().getContent() 方法:获取数据流

--- 该client访问需要认证的资源需要一个认证方法,就需要设置一个认证提供者




client的 setCredentialsProvider(bcp) 方法:设置认证提供者

BasicCredentialsProvider 类:创建认证提供者实例

setCredentials 方法:设置AuthScope和UsernamePasswordCredentials类

AuthScope类:认证范围,基于主机,端口和领域构建

UsernamePasswordCredentials:基于用户名和口令的证书,基于用户名和口令构建

Java code
// 1. 获取并设置url地址,一个字符串变量,一个URL对象 String urlStr ="http://<host>:<port>/data.json";URL url= new URL(urlStr);// 2. 创建一个密码证书,(UsernamePasswordCredentials类)  String username="foo";  String password="bar";  UsernamePasswordCredentials upc = new UsernamePasswordCredentials(username, password);// 3. 设置认证范围 (AuthScore类)String strRealm = "<mydomain>";String strHost = url.getHost();int iPort = url.getPort();AuthScope as = new AuthScope(strHost, iPort, strRealm);// 4. 创建认证提供者(BasicCredentials类) ,基于as和upcBasicCredentialsProvider bcp=new BasicCredentialsProvider();bcp.setCredentials(as, upc);// 5. 创建Http客户端(DefaultHttpClient类)DefaultHttpClient client=new DefaultHttpClient();// 6. 为此客户端设置认证提供者client.setCredentialsProvider(bcp);// 7. 创建一个get 方法(HttpGet类),基于所访问的url地址HttpGet hg= new HttpGet(urlStr);// 8. 执行get方法并返回 responseHttpResponse  hr = client.execute(hg);// 9. 从response中取回数据,使用InputStreamReader读取Response的Entity:        String line=null;        StringBuilder builder = new StringBuilder();        BufferedReader reader = new BufferedReader(new InputStreamReader(hr.getEntity().getContent() ));        while((line = reader.readLine()) != null)  builder.append(line);        strContent=builder.toString();
------解决方案--------------------
http://blog.csdn.net/yiyaaixuexi/article/details/6615157
转到博客里了
------解决方案--------------------

Java code
        HttpGet httpRequest = new HttpGet(url);        if(!name.equals("") || !password.equals(""))        {            String userPassword = name+":"+password;                    String encoding = Base64.encode(userPassword).trim();            httpRequest.addHeader("Authorization", "Basic " + encoding);        }        httpRequest.setHeader("User-agent", USER_AGENT);        try        {            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);            if(httpResponse.getStatusLine().getStatusCode()==200)            {                ret = EntityUtils.toString(httpResponse.getEntity());                            }else {                Log.e(TAG,"Fail: Code: " + httpResponse.getStatusLine().getStatusCode());            }            httpRequest.abort();        }catch(IOException  ex)        {            Log.e(TAG,ex.toString());        }
  相关解决方案