Android 客户端使用HttpGet访问服务器

 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) {

    }
   }
  } 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基本认证,用于需要认证访问的服务器。

④Base64.NO_WRAP介绍

你可能感兴趣的:(Android 客户端使用HttpGet访问服务器)