httpClientUtil的get请求

这个是httpClient的get请求写法,一般需求应该是够用了。
参考了几个博客的代码,具体的网址忘记了,有问题可以联系我。
代码:
  1 package com.demo.service.impl;
  2 
  3 import com.demo.service.HttpRequestGetService;
  4 import com.demo.utils.CloseHttpUtil;
  5 import org.apache.http.HttpEntity;
  6 import org.apache.http.ParseException;
  7 import org.apache.http.client.ClientProtocolException;
  8 import org.apache.http.client.methods.CloseableHttpResponse;
  9 import org.apache.http.client.methods.HttpGet;
 10 import org.apache.http.client.utils.URIBuilder;
 11 import org.apache.http.impl.client.CloseableHttpClient;
 12 import org.apache.http.impl.client.HttpClients;
 13 import org.apache.http.util.EntityUtils;
 14 import org.springframework.stereotype.Component;
 15 
 16 import java.io.IOException;
 17 import java.net.URISyntaxException;
 18 import java.util.Map;
 19 
 20 @Component
 21 public class HttpRequestGetServiceImpl implements HttpRequestGetService {
 22     CloseHttpUtil closeHttpUtil = new CloseHttpUtil();
 23 
 24     public String doGetRequest(String url, Map<String, String> headMap, Map<String, String> paramMap) {
 25         // 获取连接客户端工具
 26         CloseableHttpClient httpClient = HttpClients.createDefault();
 27         String entityStr = null;
 28         CloseableHttpResponse response = null;
 29         try {
 30             /*
 31              * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
 32              */
 33             URIBuilder uriBuilder = new URIBuilder(url);
 34             for (Map.Entry<String, String> param : paramMap.entrySet()) {
 35                 uriBuilder.addParameter(param.getKey(), param.getValue());
 36             }
 37             // 根据带参数的URI对象构建GET请求对象
 38             HttpGet httpGet = new HttpGet(uriBuilder.build());
 39 
 40         /*
 41          * 添加请求头信息
 42          */
 43             for (Map.Entry<String, String> header : headMap.entrySet()) {
 44                 httpGet.addHeader(header.getKey(), header.getValue());
 45             }
 46             /*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8");
 47             httpGet.addHeader("User-Identify","12345678905030000000");*/
 48             closeHttpUtil.setTimeOut(httpGet);
 49             // 执行请求
 50             response = httpClient.execute(httpGet);
 51             // 获得响应的实体对象
 52             HttpEntity entity = response.getEntity();
 53             // 使用Apache提供的工具类进行转换成字符串
 54             entityStr = EntityUtils.toString(entity, "UTF-8");
 55         } catch (ClientProtocolException e) {
 56             System.err.println("Http协议出现问题");
 57             e.printStackTrace();
 58         } catch (ParseException e) {
 59             System.err.println("解析错误");
 60             e.printStackTrace();
 61         } catch (URISyntaxException e) {
 62             System.err.println("URI解析异常");
 63             e.printStackTrace();
 64         } catch (IOException e) {
 65             System.err.println("IO异常");
 66             e.printStackTrace();
 67         } finally {
 68             // 释放连接
 69             closeHttpUtil.close(response, httpClient);
 70         }
 71 
 72         return entityStr;
 73     }
 74 
 75     public String doGetRequestNoparam(String url, Map<String, String> headMap) {
 76         // 获取连接客户端工具
 77         CloseableHttpClient httpClient = HttpClients.createDefault();
 78         String entityStr = null;
 79         CloseableHttpResponse response = null;
 80         try {
 81             /*
 82              * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
 83              */
 84             URIBuilder uriBuilder = new URIBuilder(url);
 85             // 根据带参数的URI对象构建GET请求对象
 86             HttpGet httpGet = new HttpGet(uriBuilder.build());
 87         /*
 88          * 添加请求头信息
 89          */
 90             for (Map.Entry<String, String> header : headMap.entrySet()) {
 91                 httpGet.addHeader(header.getKey(), header.getValue());
 92             }
 93             /*httpGet.addHeader("Content-Type", "application/VIID+JSON;charset=utf8");
 94             httpGet.addHeader("User-Identify","12345678905030000000");*/
 95             closeHttpUtil.setTimeOut(httpGet);
 96             // 执行请求
 97             response = httpClient.execute(httpGet);
 98             // 获得响应的实体对象
 99             HttpEntity entity = response.getEntity();
100             // 使用Apache提供的工具类进行转换成字符串
101             entityStr = EntityUtils.toString(entity, "UTF-8");
102         } catch (ClientProtocolException e) {
103             System.err.println("Http协议出现问题");
104             e.printStackTrace();
105         } catch (ParseException e) {
106             System.err.println("解析错误");
107             e.printStackTrace();
108         } catch (URISyntaxException e) {
109             System.err.println("URI解析异常");
110             e.printStackTrace();
111         } catch (IOException e) {
112             System.err.println("IO异常");
113             e.printStackTrace();
114         } finally {
115             // 释放连接
116             closeHttpUtil.close(response, httpClient);
117         }
118         return entityStr;
119     }
120 
121     public String doGetRequestStringParam(String url, Map<String, String> headMap, String param) {
122         // 获取连接客户端工具
123         CloseableHttpClient httpClient = HttpClients.createDefault();
124         String entityStr = null;
125         CloseableHttpResponse response = null;
126         try {
127             /*
128              * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
129              */
130             URIBuilder uriBuilder = new URIBuilder(url);
131             // 根据带参数的URI对象构建GET请求对象
132             HttpGet httpGet = new HttpGet(uriBuilder.build() + param);
133             System.out.println("调用String参数的URL:" + httpGet);
134         /*
135          * 添加请求头信息
136          */
137             for (Map.Entry<String, String> header : headMap.entrySet()) {
138                 httpGet.addHeader(header.getKey(), header.getValue());
139             }
140             closeHttpUtil.setTimeOut(httpGet);
141             response = httpClient.execute(httpGet);
142             // 获得响应的实体对象
143             HttpEntity entity = response.getEntity();
144             // 使用Apache提供的工具类进行转换成字符串
145             entityStr = EntityUtils.toString(entity, "UTF-8");
146         } catch (ClientProtocolException e) {
147             System.err.println("Http协议出现问题");
148             e.printStackTrace();
149         } catch (ParseException e) {
150             System.err.println("解析错误");
151             e.printStackTrace();
152         } catch (URISyntaxException e) {
153             System.err.println("URI解析异常");
154             e.printStackTrace();
155         } catch (IOException e) {
156             System.err.println("IO异常");
157             e.printStackTrace();
158         } finally {
159             // 释放连接
160             closeHttpUtil.close(response, httpClient);
161 
162         }
163         return entityStr;
164     }
165 
166     public String doGetRequestIntParam(String url, Map<String, String> headMap, int RecordStartNo, int PageRecordNum) {
167         // 获取连接客户端工具
168         CloseableHttpClient httpClient = HttpClients.createDefault();
169         String entityStr = null;
170         CloseableHttpResponse response = null;
171         try {
172             /*
173              * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
174              */
175             URIBuilder uriBuilder = new URIBuilder(url);
176             // 根据带参数的URI对象构建GET请求对象
177             HttpGet httpGet = new HttpGet(uriBuilder.build() + "?" + "RecordStartNo=" + RecordStartNo + "&" + "PageRecordNum=" + PageRecordNum);
178             System.out.println("调用String参数的URL:" + httpGet);
179         /*
180          * 添加请求头信息
181          */
182             for (Map.Entry<String, String> header : headMap.entrySet()) {
183                 httpGet.addHeader(header.getKey(), header.getValue());
184             }
185             closeHttpUtil.setTimeOut(httpGet);
186             response = httpClient.execute(httpGet);
187             // 获得响应的实体对象
188             HttpEntity entity = response.getEntity();
189             // 使用Apache提供的工具类进行转换成字符串
190             entityStr = EntityUtils.toString(entity, "UTF-8");
191         } catch (ClientProtocolException e) {
192             System.err.println("Http协议出现问题");
193             e.printStackTrace();
194         } catch (ParseException e) {
195             System.err.println("解析错误");
196             e.printStackTrace();
197         } catch (URISyntaxException e) {
198             System.err.println("URI解析异常");
199             e.printStackTrace();
200         } catch (IOException e) {
201             System.err.println("IO异常");
202             e.printStackTrace();
203         } finally {
204             // 释放连接
205             closeHttpUtil.close(response, httpClient);
206         }
207         return entityStr;
208     }
209 }
__________________________________________________________________
 1 import org.apache.http.client.config.RequestConfig;
 2 import org.apache.http.client.methods.CloseableHttpResponse;
 3 import org.apache.http.client.methods.HttpGet;
 4 import org.apache.http.client.methods.HttpPut;
 5 import org.apache.http.impl.client.CloseableHttpClient;
 6 
 7 import java.io.IOException;
 8 
 9 public class CloseHttpUtil {
10     public static void close(CloseableHttpResponse response, CloseableHttpClient httpClient) {
11         if (null != response) {
12             try {
13                 response.close();
14                 httpClient.close();
15             } catch (IOException e) {
16                 System.err.println("释放连接出错");
17                 e.printStackTrace();
18             }
19         }
20     }
21 
22     public static void setTimeOut(HttpGet httpGet) {
23         RequestConfig requestConfig = RequestConfig.custom()
24                 // 设置连接超时时间(单位毫秒)
25                 .setConnectTimeout(10000)
26                 // 设置请求超时时间(单位毫秒)
27                 .setConnectionRequestTimeout(10000)
28                 // socket读写超时时间(单位毫秒)
29                 .setSocketTimeout(10000)
30                 // 设置是否允许重定向(默认为true)
31                 .setRedirectsEnabled(true).build();
32 
33         // 将上面的配置信息 运用到这个Get请求里
34         httpGet.setConfig(requestConfig);
35     }

 

posted @ 2019-08-09 17:54  汪全意  阅读(3044)  评论(0编辑  收藏  举报