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

相關文章
相關標籤/搜索