博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClientUitl工具类
阅读量:5458 次
发布时间:2019-06-15

本文共 5186 字,大约阅读时间需要 17 分钟。

public class HttpClient {
private CloseableHttpClient httpClient; public HttpClient() {
this.httpClient = HttpClients.createDefault(); } /** * 带参数的get请求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doGet(String url, Map
map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url); if (map != null) {
for (Map.Entry
entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); } } HttpGet httpGet = new HttpGet(uriBuilder.build()); CloseableHttpResponse response = this.httpClient.execute(httpGet); HttpResult httpResult = null; if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } return httpResult; } /** * 不带参数的get请求 * * @param url * @return * @throws Exception */ public HttpResult doGet(String url) throws Exception {
HttpResult httpResult = this.doGet(url, null); return httpResult; } /** * 带参数的post请求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doPost(String url, Map
map) throws Exception {
HttpPost httpPost = new HttpPost(url); if (map != null) {
List
params = new ArrayList
(); for (Map.Entry
entry : map.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8"); httpPost.setEntity(formEntity); } CloseableHttpResponse response = this.httpClient.execute(httpPost); HttpResult httpResult = null; if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } return httpResult; } /** * 不带参数的post请求 * * @param url * @return * @throws Exception */ public HttpResult doPost(String url) throws Exception { HttpResult httpResult = this.doPost(url, null); return httpResult; } /** * 带参数的Put请求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doPut(String url, Map
map) throws Exception { HttpPut httpPut = new HttpPut(url); if (map != null) { List
params = new ArrayList
(); for (Map.Entry
entry : map.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8"); httpPut.setEntity(formEntity); } CloseableHttpResponse response = this.httpClient.execute(httpPut); HttpResult httpResult = null; if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } return httpResult; } /** * 带参数的Delete请求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doDelete(String url, Map
map) throws Exception { URIBuilder uriBuilder = new URIBuilder(url); if (map != null) { for (Map.Entry
entry : map.entrySet()) { uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); } } HttpDelete httpDelete = new HttpDelete(uriBuilder.build()); CloseableHttpResponse response = this.httpClient.execute(httpDelete); HttpResult httpResult = null; if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } return httpResult; } /** * post请求(用于请求json格式的参数) * @param url * @param params * @return */ public static String doPostJson(String url, String params) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url);// 创建httpPost httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-Type", "application/json"); String charSet = "UTF-8"; StringEntity entity = new StringEntity(params, charSet); httpPost.setEntity(entity); CloseableHttpResponse response = null; try { response = httpclient.execute(httpPost); StatusLine status = response.getStatusLine(); int state = status.getStatusCode(); if (state == HttpStatus.SC_OK) { HttpEntity responseEntity = response.getEntity(); String jsonString = EntityUtils.toString(responseEntity); return jsonString; } else{ logger.error("请求返回:"+state+"("+url+")"); } } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }

转载于:https://www.cnblogs.com/h-c-g/p/9978849.html

你可能感兴趣的文章
HttpClientUitl工具类
查看>>
Could not find or load main class
查看>>
VC 预定义宏
查看>>
indexOf()
查看>>
dom4j对xml读取操作
查看>>
Yii2.0实现微信公众号后台开发
查看>>
Shell 传递参数
查看>>
Ibatis 泛型化dao模版
查看>>
hrbust 1133 (kruskal)
查看>>
vue 接口统一管理
查看>>
margin 相关 bug 系列
查看>>
模拟+贪心 SCU 4445 Right turn
查看>>
2012 Multi-University #7
查看>>
第五章 循环结构反思
查看>>
WebConfig配置文件有哪些不为人知的秘密?
查看>>
自动控制原理的三不管地带之——开闭环函数特征方程原理
查看>>
HDU 2001 计算亮点间的距离
查看>>
spring学习笔记--quartz和定时任务执行
查看>>
ASP.NET页面刷新样式改变解决方法
查看>>
Redis- 简单操作命令
查看>>