HttpClient请求工具类

2019-01-05 13:10:07来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 package com.yangche.utils;
  2 
  3 import org.apache.http.NameValuePair;
  4 import org.apache.http.client.ClientProtocolException;
  5 import org.apache.http.client.entity.UrlEncodedFormEntity;
  6 import org.apache.http.client.methods.*;
  7 import org.apache.http.client.utils.URIBuilder;
  8 import org.apache.http.entity.ContentType;
  9 import org.apache.http.entity.StringEntity;
 10 import org.apache.http.impl.client.CloseableHttpClient;
 11 import org.apache.http.impl.client.HttpClients;
 12 import org.apache.http.message.BasicNameValuePair;
 13 import org.apache.http.util.EntityUtils;
 14 import org.springframework.util.StringUtils;
 15 
 16 import java.io.IOException;
 17 import java.io.UnsupportedEncodingException;
 18 import java.net.URI;
 19 import java.net.URISyntaxException;
 20 import java.util.ArrayList;
 21 import java.util.List;
 22 import java.util.Map;
 23 
 24 public class HttpClientUtil {
 25     public static String doGet(String url, Map<String,Object> param){
 26         //创建httpclient对象
 27         CloseableHttpClient httpClient = HttpClients.createDefault();
 28         String resultString="";
 29         CloseableHttpResponse response=null;
 30         try {
 31             URIBuilder builder=new URIBuilder(url);
 32             if(param!=null){
 33                 for (String key:param.keySet()){
 34                     if(param.get(key) instanceof List){
 35                         List list=(List)param.get(key);
 36                         for (Object liString:list){
 37                             builder.addParameter(key,String.valueOf(liString));
 38                         }
 39                     }else {
 40                         builder.addParameter(key,String.valueOf(param.get(key)));
 41                     }
 42                 }
 43             }
 44             URI uri=builder.build();
 45             //创建httpGet请求
 46             HttpGet httpGet=new HttpGet(uri);
 47             //执行请求
 48             response=httpClient.execute(httpGet);
 49             resultString= EntityUtils.toString(response.getEntity(),"UTF-8");
 50         } catch (URISyntaxException e) {
 51             e.printStackTrace();
 52         } catch (ClientProtocolException e) {
 53             e.printStackTrace();
 54         } catch (IOException e) {
 55             e.printStackTrace();
 56         }finally {
 57             try {
 58                 if(response!=null){
 59                     response.close();
 60                 }
 61                 httpClient.close();
 62             } catch (IOException e) {
 63                 e.printStackTrace();
 64             }
 65         }
 66         return resultString;
 67     }
 68     public static String doGet(String url){
 69         return doGet(url,null);
 70     }
 71     public static String doPost(String url,Map<String,String> param){
 72         //创建httpclient对象
 73         CloseableHttpClient httpClient = HttpClients.createDefault();
 74         CloseableHttpResponse response=null;
 75         String resultString="";
 76         //创建httpPost请求
 77         HttpPost httpPost = new HttpPost(url);
 78         //创建参数列表
 79         if(param!=null){
 80             List<NameValuePair> paramList=new ArrayList<>();
 81             for (String key:param.keySet()){
 82                 paramList.add(new BasicNameValuePair(key,param.get(key)));
 83             }
 84             try {
 85                 //模拟表单
 86                 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(paramList);
 87                 httpPost.setEntity(entity);
 88                 //执行http请求
 89                 response=httpClient.execute(httpPost);
 90                 resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
 91             } catch (UnsupportedEncodingException e) {
 92                 e.printStackTrace();
 93             } catch (ClientProtocolException e) {
 94                 e.printStackTrace();
 95             } catch (IOException e) {
 96                 e.printStackTrace();
 97             }finally {
 98                 try {
 99                     response.close();
100                 } catch (IOException e) {
101                     e.printStackTrace();
102                 }
103             }
104         }
105         return resultString;
106     }
107     public static String doPost(String url){
108         return doPost(url,null);
109     }
110 
111     /**
112      * restful请求
113      * @param url
114      * @param requestType 请求类型:post、delete、patch
115      * @param json
116      * @return
117      */
118     public static String doRequest(String url,String requestType,String json){
119         //创建HttpClient对象
120         CloseableHttpClient httpClient=HttpClients.createDefault();
121         CloseableHttpResponse response=null;
122         String resultString="";
123         try {
124             //创建不同类型的请求
125             HttpPost httpPost=new HttpPost(url);
126             HttpDeleteWithBody httpDelete =new HttpDeleteWithBody(url);
127             HttpPatch httpPatch=new HttpPatch(url);
128             //创建请求内容
129             StringEntity entity=new StringEntity(json, ContentType.APPLICATION_JSON);
130             //执行http请求
131             if(!StringUtils.isEmpty(requestType)&&requestType.equalsIgnoreCase("post")){
132                 httpPost.setEntity(entity);
133                 response=httpClient.execute(httpPost);
134             }else if(!StringUtils.isEmpty(requestType)&&requestType.equalsIgnoreCase("delete")){
135                 httpDelete.setEntity(entity);
136                 response=httpClient.execute(httpDelete);
137             }else if(!StringUtils.isEmpty(requestType)&&requestType.equalsIgnoreCase("patch")){
138                 httpPatch.setEntity(entity);
139                 response=httpClient.execute(httpPatch);
140             }
141             resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
142         } catch (IOException e) {
143             e.printStackTrace();
144         } finally {
145             try {
146                 response.close();
147             } catch (IOException e) {
148                 e.printStackTrace();
149             }
150         }
151         return resultString;
152     }
153 
154     private static class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
155         public static final String METHOD_NAME="DELETE";
156         @Override
157         public String getMethod() {
158             return METHOD_NAME;
159         }
160         public HttpDeleteWithBody (final String uri){
161             super();
162             setURI(URI.create(uri));
163         }
164         public HttpDeleteWithBody (final URI uri){
165             super();
166             setURI(uri);
167         }
168         public HttpDeleteWithBody(){
169             super();
170         }
171     }
172 }
以上所需的maven依赖如下:
 1 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
 2 <dependency>
 3     <groupId>org.apache.httpcomponents</groupId>
 4     <artifactId>httpcore</artifactId>
 5     <version>4.4.10</version>
 6 </dependency>
 7 
 8 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
 9 <dependency>
10     <groupId>org.apache.httpcomponents</groupId>
11     <artifactId>httpclient</artifactId>
12     <version>4.5.6</version>
13 </dependency>

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring Boot学习大全(入门)

下一篇:javaSE练习2——流程控制_2.1