扣丁学堂——Apache HTTP Client


一丶コードは本文を参照
二丶アナログをスキップしてログインするのは、この後期がもっと強化されていると思っているので、後で勉強をスキップする可能性があります.
扣丁学堂——Apache HTTP Client_第1张图片
扣丁学堂——Apache HTTP Client_第2张图片
//      List,      URL    
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();  
params.add(new BasicNameValuePair("param1", "  "));  
params.add(new BasicNameValuePair("param2", "value2"));  

//       
String param = URLEncodedUtils.format(params, "UTF-8");  

//baseUrl             
String baseUrl = "http://ubs.free4lab.com/php/method.php";  

// URL       
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);  

HttpClient httpClient = new DefaultHttpClient();  

try {  
    HttpResponse response = httpClient.execute(getMethod); //  GET    

    Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //       
    Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//           
} catch (ClientProtocolException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}  

扣丁学堂——Apache HTTP Client_第3张图片
/ GET    ,      List  
params = new LinkedList<BasicNameValuePair>();  
params.add(new BasicNameValuePair("param1", "Post  "));  
params.add(new BasicNameValuePair("param2", "     "));  

try {  
    HttpPost postMethod = new HttpPost(baseUrl);  
    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //     POST Entity   

    HttpResponse response = httpClient.execute(postMethod); //  POST    
    Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //       
    Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //        

} catch (UnsupportedEncodingException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (ClientProtocolException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
} catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}