HttpClient4.X发送带参数的Get请求

原地址:http://blog.csdn.net/zknxx/article/details/51598852

HttpClient 是apache 组织下面的一个用于处理HTTP 请求和响应的开源工具。所用jar包为httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar、commons-codec-1.6.jar。

发送Get请求代码如下:

[java]  view plain  copy
  1. package com.zkn.newlearn.httpclient;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.util.List;  
  8.   
  9. import org.apache.http.Consts;  
  10. import org.apache.http.HttpEntity;  
  11. import org.apache.http.NameValuePair;  
  12. import org.apache.http.ParseException;  
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  14. import org.apache.http.client.methods.CloseableHttpResponse;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.impl.client.CloseableHttpClient;  
  17. import org.apache.http.impl.client.HttpClients;  
  18. import org.apache.http.message.BasicNameValuePair;  
  19. import org.apache.http.util.EntityUtils;  
  20.   
  21. import com.google.common.collect.Lists;  
  22.   
  23. public class HttpClientTest02 {  
  24.   
  25.     public static void main(String[] args) {  
  26.         CloseableHttpClient httpClient = HttpClients.createDefault();  
  27.         CloseableHttpResponse response = null;  
  28.         InputStream is = null;  
  29.         String url = "http://localhost:8080/MyWebxTest/getCityByProvinceEname.do";  
  30.         //封装请求参数  
  31.         List params = Lists.newArrayList();  
  32.         params.add(new BasicNameValuePair("cityEname""henan"));  
  33.         String str = "";  
  34.         try {  
  35.             //转换为键值对  
  36.             str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));  
  37.             System.out.println(str);  
  38.             //创建Get请求  
  39.             HttpGet httpGet = new HttpGet(url+"?"+str);  
  40.             //执行Get请求,  
  41.             response = httpClient.execute(httpGet);  
  42.             //得到响应体  
  43.             HttpEntity entity = response.getEntity();  
  44.             if(entity != null){  
  45.                 is = entity.getContent();  
  46.                 //转换为字节输入流  
  47.                 BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));  
  48.                 String body = null;  
  49.                 while((body=br.readLine()) != null){  
  50.                     System.out.println(body);  
  51.                 }  
  52.             }  
  53.         } catch (ParseException e) {  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         } finally{  
  58.             //关闭输入流,释放资源  
  59.             if(is != null){  
  60.                 try {  
  61.                     is.close();  
  62.                 } catch (IOException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.             //消耗实体内容  
  67.             if(response != null){  
  68.                 try {  
  69.                     response.close();  
  70.                 } catch (IOException e) {  
  71.                     e.printStackTrace();  
  72.                 }  
  73.             }  
  74.             //关闭相应 丢弃http连接  
  75.             if(httpClient != null){  
  76.                 try {  
  77.                     httpClient.close();  
  78.                 } catch (IOException e) {  
  79.                     e.printStackTrace();  
  80.                 }  
  81.             }  
  82.         }  
  83.           
  84.     }  
  85. }  

你可能感兴趣的:(HttpClient4.X发送带参数的Get请求)