Android终端实现Http的摘要(Digest)认证

wqhjfree 2011-07-18 05:00:13

请问, 如何在Android终端实现Http的摘要(Digest)认证, 到网上搜了下, 都说的很笼统, 有没找到相关的API. 请高人指教.
...全文
514 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦帆帆 2011-07-21
  • 打赏
  • 举报
回复
正要用到这个东西,学习了。
念茜 2011-07-19
  • 打赏
  • 举报
回复
wqhjfree 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 wqhjfree 的回复:]
请问, 如何在Android终端实现Http的摘要(Digest)认证, 到网上搜了下, 都说的很笼统, 有没找到相关的API. 请高人指教.
[/Quote]
请问到哪收到的. 麻烦提供下URL , 多谢啦.
wqhjfree 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hjue 的回复:]
Java code

HttpGet httpRequest = new HttpGet(url);
if(!name.equals("") || !password.equals(""))
{
String userPassword = name+":"+password;
Stri……
[/Quote]
1.请问有没有服务器和客户端的完整例子.
2.有没有摘要(HTTP Digest)认证的例子.
多鱼的夏天 2011-07-19
  • 打赏
  • 举报
回复


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

base64

public class Base64 {

public final static String BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +

"abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/";

public final static int SPLIT_LINES_AT = 76;

public static byte[] zeroPad(int length, byte[] bytes) {
byte[] padded = new byte[length]; // initialized to zero by JVM
System.arraycopy(bytes, 0, padded, 0, bytes.length);
return padded;
}

public static String encode(String string) {

String encoded = "";
byte[] stringArray;
try {
stringArray = string.getBytes("UTF-8"); // use appropriate encoding string!
} catch (Exception ignored) {
stringArray = string.getBytes(); // use locale default rather than croak
}
// determine how many padding bytes to add to the output
int paddingCount = (3 - (stringArray.length % 3)) % 3;
// add any necessary padding to the input
stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
// process 3 bytes at a time, churning out 4 output bytes
// worry about CRLF insertions later
for (int i = 0; i < stringArray.length; i += 3) {
int j = (stringArray[i] << 16) + (stringArray[i + 1] << 8) +
stringArray[i + 2];
encoded = encoded + BASE64CODE.charAt((j >> 18) & 0x3f) +
BASE64CODE.charAt((j >> 12) & 0x3f) +
BASE64CODE.charAt((j >> 6) & 0x3f) +
BASE64CODE.charAt(j & 0x3f);
}
// replace encoded padding nulls with "="
String ret = splitLines(encoded.substring(0, encoded.length() -
paddingCount) + "==".substring(0, paddingCount));
return ret.trim();

}
public static String splitLines(String string) {

String lines = "";
for (int i = 0; i < string.length(); i += SPLIT_LINES_AT) {

lines += string.substring(i, Math.min(string.length(), i + SPLIT_LINES_AT));
lines += "\r\n";

}
return lines;

}


}
念茜 2011-07-18
  • 打赏
  • 举报
回复
我搜了一下资料,我没有自己实践过,还需要你亲自验证一下才好

基本思路:

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

DefualtHttpClient 类:创建client对象

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

HttpGet类:基于url创建HttpGet对象

HttpResponse类: execute的返回值

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

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




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

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

setCredentials 方法:设置AuthScope和UsernamePasswordCredentials类

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

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


// 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和upc

BasicCredentialsProvider 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方法并返回 response

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




80,470

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧