Android终端实现Http的摘要(Digest)认证 请问, 如何在Android终端实现Http的摘要(Digest)认证, 到网上搜了下, 都说的很笼统, 有没找到相关的API. 请高人指教. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我搜了一下资料,我没有自己实践过,还需要你亲自验证一下才好基本思路:--- 使用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和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(); 请问到哪收到的. 麻烦提供下URL , 多谢啦. http://blog.csdn.net/yiyaaixuexi/article/details/6615157转到博客里了 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()); }base64public 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; } } 1.请问有没有服务器和客户端的完整例子. 2.有没有摘要(HTTP Digest)认证的例子. ubuntu 下编译android 2.3 报错stubs-32.h找不到 用android编写的两个activity事件之间的转换问题, 怎样在已经启动的AVD上运行程序 关于Android启动第二屏背景音乐 求助关于android的SQLite的问题。 怎样改变listView的选中的item的背景 恭祝 Android 开版 android本地数据库备份与恢复怎么搞,有何思路? ListView中,如何在一个数组中添加两个item openl 读取obj贴图出错 怎么获得Canvas中的一个像素点的颜色值 求教:有没有办法给项目中每一个按钮加入等待对话框,源码已上
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和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();
转到博客里了
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());
}base64public 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;
}
}
2.有没有摘要(HTTP Digest)认证的例子.