DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mGet = new HttpGet(urlPath);
mGet.setHeader("Authorization", "Basic "+Base64.encodeToString("bdurkee:Dazee2021".getBytes(), Base64.NO_WRAP));
try {
HttpResponse response = mHttpClient.execute(mGet);
int res = response.getStatusLine().getStatusCode();
if (res == 401) {
System.out.println("get request send and receive 401");
} else if (res == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) { html
}
}
} catch (Exception e) {
e.printStackTrace();
} 服务器
①使用HttpPost的流程大体相同,只是跟post,get有关系。
②Android API推荐使用HttpUrlConnection,再看看。
③mGet.setHeader("Authorization", "Basic "+Base64.encodeToString("bdurkee:Dazee2021".getBytes(), Base64.NO_WRAP));
HTTP Basic Authorization是Http基本认证,用于须要认证访问的服务器。 post
④Base64.NO_WRAP介绍 url