80,470
社区成员




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