Android http协议的get和post的区别

来源:互联网 发布:ftp使用的端口 编辑:程序博客网 时间:2024/06/11 06:53
 一、get请求;
      void GetDate() {  
 
    // URL 
 
   String uriAPI = "http://172.0.0.0:8082/TestServelt/login";  
 
    String idString = "110";  
 
    String nameString = "cyy";  
 
    String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;  
 
    HttpGet get = new HttpGet(url);  
 
    HttpClient client = new DefaultHttpClient();  
 
    try {  
 
     HttpResponse response = client.execute(get);// 执行Post方法  
 
        String resultString = EntityUtils.toString(response.getEntity());//得到结果  
 
    } catch (Exception e) {  
 
    }  
 
}  
 
二、post请求:
 
方式一;httppost
 
void GetDate() {  
 
    // URL  
 
    String uriAPI = "http://172.0.0.0:8082/TestServelt/login";  
 
    String idString = "110";  
 
    String nameString = "cyy"
 
   String url = uriAPI + "?" + "id=" + idString + "&name=" + nameString;  
 
    HttpGet get = new HttpGet(url);  
 
    HttpClient client = new DefaultHttpClient();  
 
    try {  
 
        HttpResponse response = client.execute(get);// 执行Post方法  
 
        String resultString = EntityUtils.toString(response.getEntity());//得到结果 
 
   } catch (Exception e) {  
 
    }  
 

方式二:HttpURLConnection
 
  1. class AsyncHttpURL extends AsyncTask<Object, Object, String> {  
  2.   
  3.     @Override  
  4.     protected String doInBackground(Object... params) {  
  5.         try {  
  6.             // 建立连接  
  7.             URL url = new URL(uriAPI);  
  8.             HttpURLConnection httpConn = (HttpURLConnection) url  
  9.                     .openConnection();  
  10.   
  11.             httpConn.setConnectTimeout(5 * 1000);  
  12.             httpConn.setReadTimeout(10 * 1000);  
  13.             // //设置连接属性  
  14.   
  15.             // 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true  
  16.             httpConn.setDoOutput(true);  
  17.   
  18.             // 使用 URL 连接进行输入, Default is true.  
  19.             httpConn.setDoInput(true);  
  20.   
  21.             // Post 请求不能使用缓存,忽略缓存  
  22.             httpConn.setUseCaches(false);  
  23.   
  24.             // 是成员函数,仅作用于当前函数  
  25.             httpConn.setInstanceFollowRedirects(true);  
  26.   
  27.             // 设置URL请求方法, Default is GET  
  28.             httpConn.setRequestMethod("POST");  
  29.   
  30.             // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的  
  31.             // 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码  
  32.             httpConn.setRequestProperty("Content-Type",  
  33.                     "application/x-www-form-urlencoded;charset=UTF-8");  
  34.   
  35.             // 编码方式,上面设置了charset=UTF-8,这里就不用设置了  
  36.             httpConn.setRequestProperty("Charset""UTF-8");  
  37.   
  38.             // 维持长连接  
  39.             httpConn.setRequestProperty("Connection""Keep-Alive");  
  40.   
  41.             // 要注意的是connection.getOutputStream会隐含的进行connect。  
  42.             httpConn.connect();  
  43.   
  44.             DataOutputStream out = new DataOutputStream(  
  45.                     httpConn.getOutputStream());  
  46.             // The URL-encoded contend  
  47.             // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致,传多参请看下面方法  
  48.             String content = "name="+ URLEncoder.encode("cyy""utf-8");  
  49.             // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面  
  50.             out.writeBytes(content);  
  51.   
  52.             out.flush();  
  53.             out.close();   
  54.             BufferedReader reader = new BufferedReader(  
  55.                     new InputStreamReader(httpConn.getInputStream()));  
  56.             StringBuilder sb = new StringBuilder();  
  57.             String lineTxt = null;  
  58.             while ((lineTxt = reader.readLine()) != null) {  
  59.                 sb.append(lineTxt);  
  60.             }  
  61.             reader.close();  
  62.             httpConn.disconnect();  
  63.             return sb.toString();  
  64.         } catch (Exception ex) {  
  65.             ex.printStackTrace();  
  66.             return ex.toString();  
  67.         }  
  68.   
  69.     }  
  70.   
  71.     @Override  
  72.     protected void onPostExecute(String result) {  
  73.         tv.setText(result);  
  74.     }  
  75.   
  76. }  
  77. NameValuePair转换String方法:  
  78. /** NameValuePair转换String */  
  79. private String getReqString(List<? extends NameValuePair> params) {  
  80.     // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致  
  81.     StringBuffer req = new StringBuffer("");  
  82.     if (null != params && !params.isEmpty()) {  
  83.         NameValuePair pair;  
  84.         pair = params.get(0);  
  85.         try {  
  86.             req.append(pair.getName() + "="  
  87.                     + URLEncoder.encode(pair.getValue(), "utf-8"));  
  88.             for (int i = 1; i < params.size(); i++) {  
  89.                 pair = params.get(i);  
  90.                 req.append("&" + pair.getName() + "="  
  91.                         + URLEncoder.encode(pair.getValue(), "utf-8"));  
  92.             }  
  93.         } catch (UnsupportedEncodingException e) {  
  94.         }  
  95.     }  
  96.     return req.toString();  
  97. }