HttpClient发送Get、Post请求简单实践


HttpClient发送Get、Post请求简单实践

https://blog.csdn.net/swordgirl2011/article/details/53297146

1. 配置及实例化HttpClient:

 

[java] view plain copy
 
  1. private static final CloseableHttpClient httpclient;  
  2.     public static final String CHARSET = "UTF-8";  
  3.       
  4.       
  5.     static{  
  6.         RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(3000).build();  
  7.         httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();  
  8.     }  


2. 发送Get请求方法:

 

 

[java] view plain copy
 
  1. /** 
  2.      * HTTP Get 获取内容 
  3.      * @param url请求的url地址 ?之前的地址 
  4.      * @param params请求的参数 
  5.      * @param charset编码格式 
  6.      * @return 页面内容 
  7.      */  
  8.     public static String sendGet(String url, Map<String, Object> params) throws ParseException, UnsupportedEncodingException, IOException{  
  9.           
  10.         if(params !=null && !params.isEmpty()){  
  11.               
  12.             List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());  
  13.               
  14.             for (String key :params.keySet()){  
  15.                 pairs.add(new BasicNameValuePair(key, params.get(key).toString()));  
  16.             }  
  17.             url +="?"+EntityUtils.toString(new UrlEncodedFormEntity(pairs), CHARSET);  
  18.         }  
  19.           
  20.         HttpGet httpGet = new HttpGet(url);  
  21.         CloseableHttpResponse response = httpclient.execute(httpGet);  
  22.         int statusCode = response.getStatusLine().getStatusCode();  
  23.         if(statusCode !=200){  
  24.             httpGet.abort();  
  25.             throw new RuntimeException("HttpClient,error status code :" + statusCode);  
  26.         }  
  27.         HttpEntity entity = response.getEntity();  
  28.         String result = null;  
  29.         if (entity != null) {  
  30.             result = EntityUtils.toString(entity, "utf-8");  
  31.             EntityUtils.consume(entity);  
  32.             response.close();  
  33.             return result;  
  34.         }else{  
  35.             return null;  
  36.         }  
  37.     }  


3. 发送Post请求方法:

 

 

[java] view plain copy
 
  1. /** 
  2.  * HTTP Post 获取内容 
  3.  * @param url请求的url地址 ?之前的地址 
  4.  * @param params请求的参数 
  5.  * @param charset编码格式 
  6.  * @return 页面内容 
  7.  * @throws IOException  
  8.  * @throws ClientProtocolException  
  9.  */  
  10.   
  11. public static String sendPost(String url, Map<String, Object> params) throws ClientProtocolException, IOException {  
  12.   
  13.         List<NameValuePair> pairs = null;  
  14.         if (params != null && !params.isEmpty()) {  
  15.             pairs = new ArrayList<NameValuePair>(params.size());  
  16.             for (String key : params.keySet()) {  
  17.                 pairs.add(new BasicNameValuePair(key, params.get(key).toString()));  
  18.         }  
  19.         }  
  20.         HttpPost httpPost = new HttpPost(url);  
  21.         if (pairs != null && pairs.size() > 0) {  
  22.             httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));  
  23.         }  
  24.         CloseableHttpResponse response = httpclient.execute(httpPost);  
  25.         int statusCode = response.getStatusLine().getStatusCode();  
  26.         if (statusCode != 200) {  
  27.             httpPost.abort();  
  28.             throw new RuntimeException("HttpClient,error status code :" + statusCode);  
  29.         }  
  30.         HttpEntity entity = response.getEntity();  
  31.         String result = null;  
  32.         if (entity != null) {  
  33.             result = EntityUtils.toString(entity, "utf-8");  
  34.             EntityUtils.consume(entity);  
  35.             response.close();  
  36.             return result;  
  37.         }else{  
  38.              return null;  
  39.         }  
  40. }  


4. 编写测试用例,进行Get 、Post请求方法的测试

 

4.1 测试Get方法,e .g:

 

[java] view plain copy
 
  1. @Test  
  2. public void testGet() throws ParseException, UnsupportedEncodingException, IOException {  
  3.     String testUrl01 = "具体的测试接口地址";  
  4.     Map<String, Object> params01 = new HashMap<>();  
  5.     params01.put("参数01", "对应的参数取值");  
  6.     params01.put("参数02", "<span style="font-family:Arial, Helvetica, sans-serif;">对应的参数取值</span><span style="font-family:Arial, Helvetica, sans-serif;">");</span>  
  7.     System.out.println(sendGet(testUrl01, params01));  
  8.       
  9. }  

 

4.2 测试Post方法,e.g:

 

[java] view plain copy
 
  1. @Test  
  2.     public void testPost() throws ClientProtocolException, IOException {  
  3.       
  4.         String testUrl02 = "具体的测试接口地址";  
  5.         Map<String, Object> params02 = new HashMap<>();  
  6.         params02.put("参数01", "对应的参数取值");  
  7.         params02.put("参数02", "对应的参数取值");  
  8.         System.out.println(sendPost(testUrl02, params02));  
  9.           
  10.     }  

 

 

最后

资料参考:http://blog.csdn.net/nevergiveuplzl/article/details/52304266

posted @ 2018-05-17 09:55  wjj1013  阅读(299)  评论(0编辑  收藏  举报