HttpClient模块的HttpGet和HttpPost

最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有被deprecated。去看了下官方文档,确实不推荐使用了,点击此处详情。

  • DefaultHttpClient —> CloseableHttpClient
  • HttpResponse —> CloseableHttpResponse
[html] view plain copy print?
  1.   
  2. <dependency>  
  3.     <groupId>org.apache.httpcomponentsgroupId>  
  4.     <artifactId>httpclientartifactId>  
  5.     <version>4.3version>  
  6. dependency>  

新的用法如下,如下。
[java] view plain copy print?
  1. package com.somnus.http;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  12. import org.apache.http.client.methods.CloseableHttpResponse;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.impl.client.CloseableHttpClient;  
  16. import org.apache.http.impl.client.HttpClients;  
  17. import org.apache.http.message.BasicNameValuePair;  
  18. import org.apache.http.protocol.HTTP;  
  19. import org.apache.http.util.EntityUtils;  
  20. import org.junit.Test;  
  21. import org.slf4j.Logger;  
  22. import org.slf4j.LoggerFactory;  
  23.   
  24. /** 
  25.  * @Description: TODO 
  26.  * @author Somnus 
  27.  * @date 2015年12月16日 下午2:37:26 
  28.  * @version V1.0 
  29.  */  
  30. public class HttpClient {  
  31.   
  32.     private transient Logger log = LoggerFactory.getLogger(this.getClass());  
  33.   
  34.     @Test  
  35.     public void doGet() throws IOException {  
  36.         String uriAPI = "http://localhost:8080/SpringMVC/account/json";  
  37.         String result = "";  
  38.         // HttpGet httpRequst = new HttpGet(URI uri);  
  39.         // HttpGet httpRequst = new HttpGet(String uri);  
  40.         // 创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。  
  41.         HttpGet httpRequst = new HttpGet(uriAPI);  
  42.         CloseableHttpResponse httpResponse = null;  
  43.         try {  
  44.             CloseableHttpClient httpclient = HttpClients.createDefault();  
  45.             httpResponse = httpclient.execute(httpRequst);  
  46.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  47.                 HttpEntity httpEntity = httpResponse.getEntity();  
  48.                 result = EntityUtils.toString(httpEntity);// 取出应答字符串  
  49.                 // 一般来说都要删除多余的字符  
  50.                 result.replaceAll("\r""");// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格  
  51.             } else  
  52.                 httpRequst.abort();  
  53.         } catch (ClientProtocolException e) {  
  54.             log.error(e.getMessage(), e);  
  55.         } catch (IOException e) {  
  56.             log.error(e.getMessage(), e);  
  57.         } finally{  
  58.             httpResponse.close();  
  59.         }  
  60.         System.out.println(result);  
  61.     }  
  62.   
  63.     @Test  
  64.     public void doPost() throws IOException {  
  65.         String uriAPI = "http://localhost:8080/SpringMVC/databind/json";// Post方式没有参数在这里  
  66.         String result = "";  
  67.         // 创建HttpPost对象  
  68.         HttpPost httpRequst = new HttpPost(uriAPI);  
  69.   
  70.         List params = new ArrayList();  
  71.         params.add(new BasicNameValuePair("username""admin"));  
  72.         params.add(new BasicNameValuePair("password""123456"));  
  73.         CloseableHttpResponse httpResponse = null;  
  74.           
  75.         try {  
  76.             httpRequst.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
  77.             CloseableHttpClient httpclient = HttpClients.createDefault();  
  78.             httpResponse = httpclient.execute(httpRequst);  
  79.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  80.                 HttpEntity httpEntity = httpResponse.getEntity();  
  81.                 result = EntityUtils.toString(httpEntity);// 取出应答字符串  
  82.             }  
  83.         } catch (UnsupportedEncodingException e) {  
  84.             log.error(e.getMessage(), e);  
  85.         } catch (ClientProtocolException e) {  
  86.             log.error(e.getMessage(), e);  
  87.         } catch (IOException e) {  
  88.             log.error(e.getMessage(), e);  
  89.         } finally{  
  90.             httpResponse.close();  
  91.         }  
  92.         System.out.println(result);  
  93.     }  

你可能感兴趣的:(WebService详解)