HttpClientUitl工具类
2024-09-01 04:01:44
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;
} }
最新文章
- python 筛选股票
- 浅析 IDE跟编译器
- 【C语言】C语言运算符
- 如何拿到半数面试公司Offer——我的Python求职之路
- jsp_包含指令
- codeforces 424D
- 【转】linux下a.out >;outfile 2>;&;1重定向问题
- InvoiceCancelSendApAction
- AjaxPro使用说明
- BZOJ2213: [Poi2011]Difference
- SpringBoot+AOP整合
- MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容
- Python: re.sub()第二个参数
- RestFul风格接口示例
- java struts2入门学习---自定义类型转换
- LeetCode——Integer to Roman
- TFIDF练习
- CentOS7.3部署镜像仓库Harbor
- 无源码情况下直接修改jar里内容思路
- flask请求和应用上下文
热门文章
- Delphi获取句柄
- NX二次开发-UFUN创建固定的基准平面UF_MODL_create_fixed_dplane
- NApache+JBOSS架构方案
- 牛客多校第六场 D move 枚举/机智题
- code rain???
- 20140319 const sizeof define 编译时分配内存
- 消息中间件kafka学习记录
- mac下xampp+vscode进行php程序调试
- ransformResourcesWithMergeJavaResForDebug问题
- curl 基础