HttpClientUitl工具类
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<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> 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<String, Object> map) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> 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<String, Object> map) throws Exception {
HttpPut httpPut = new HttpPut(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> 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<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> 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;
} }
HttpClientUitl工具类的更多相关文章
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- JAVA 日期格式工具类DateUtil.java
DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...
随机推荐
- docker快速安装elasticsearch
一.选择版本,拉取镜像 docker pull elasticsearch:5.6.9 #不选择版本就是最新的 二.运行设置容器 # -d 表示在后台运行 docker run -d -p 9200: ...
- NX二次开发-重命名装配组件
在GC工具里面是有一个重命名装配组件的命令的,除了这个外,好像没看到NX里还有其他可以重命名装配组件的命令,本来以为在UFUN ASSEM装配的头文件里会有更改装配部件名字的函数,但是没有找到,可能没 ...
- .Net Core 部署之一 《CentOS 从GitHub/Gitee 等源代码网站部署Web网站》
先看下楼主从某阿打折购买的渣渣服务器 lsb_release -a 一.安装dotnet-SDK 注册微软的服务 sudo rpm -Uvh https://packages.microsoft.co ...
- 20130313 word 公式与文字对齐
1. 就是这儿 然后是这儿 2.这种错误很可能就是进入了死循环 3.复习了字符串现场编写 1.strstr(const char *string, const char *substring):查找s ...
- ECMAScript1.1 js书写位置 | 声明变量 | 基本数据类型 | 数据类型转换 | 操作符 | 布尔类型的隐式转换
js书写位置 由于在写css样式时使用的时双引号,所以我们在写js代码时建议使用单引号(‘’)! 行内式 <input type="button" value="点 ...
- maven-dependencyManagement和dependencies区别
在多模块的maven项目中,如果各个子项目的依赖包相同但版本不同的话,对于测试.发布和管理非常困难 dependencyManagement就是起统一版本作用的一个标签,好处有2,一是统一版本,二是子 ...
- USACO2005 Cow Acrobats /// 前缀和 oj23402
题目大意: N (1 ≤ N ≤ 50,000)头牛叠罗汉 找到最优排序使所有牛的风险值总和最小 每头牛重量 (1 ≤ Wi ≤ 10,000) 力量 (1 ≤ Si ≤ 1,000,000,000) ...
- python库之mlxtend
一.安装 conda install mlxtend --channel conda-forge 具体请看参考文献一 二.入门例子 请看参考文献2上github的举例 参考文献 http://rasb ...
- android studio toolbar遮挡住下面控件内容
只需要在该控件布局(content_***.xml)加入: app:layout_behavior="@string/appbar_scrolling_view_behavior" ...
- Dom关于位置和尺寸的api
parentNode 直接父级//和offsetParent不同 inner2.parentNode <!DOCTYPE html> <html id="html&q ...