分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

HttpClientUtils

发布时间:2023-09-06 01:39责任编辑:彭小芳关键词:暂无标签

public class HttpclientUtils {

@Autowired(required = false)private CloseableHttpClient httpClient;@Autowired(required = false)private RequestConfig config;/** * 没有带参数GET请求 * ?* @param url 请求地址 * @return 返回状态码为200 返回页面内容 否则,返回null * @throws Exception */public String doget(String url) throws Exception { ???// 创建http GET请求 ???HttpGet httpGet = new HttpGet(url); ???// 设置请求参数 ???httpGet.setConfig(config); ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpGet); ???????// 判断返回状态是否为200 ???????if (response.getStatusLine().getStatusCode() == 200) { ???????????String content = EntityUtils.toString(response.getEntity(), "UTF-8"); ???????????return content; ???????} ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???????// httpClient.close(); ???} ???return null;}/** * 带有参数GET请求 * ?* @param url 请求地址 * @param params 请求参数 * @return 返回状态码为200 返回页面内容 否则,返回null * @throws Exception */public String doget(String url, Map<String, Object> params) throws Exception { ???// 定义请求的参数 ???URIBuilder uriBuilder = new URIBuilder(url); ???if (!params.isEmpty()) { ???????for (Map.Entry<String, Object> entry : params.entrySet()) { ???????????uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); ???????} ???} ???URI uri = uriBuilder.build(); ???// 创建http GET请求 ???HttpGet httpGet = new HttpGet(uri); ???// 设置请求参数 ???httpGet.setConfig(config); ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpGet); ???????// 判断返回状态是否为200 ???????if (response.getStatusLine().getStatusCode() == 200) { ???????????return EntityUtils.toString(response.getEntity(), "UTF-8"); ???????} ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???????// httpclient.close(); ???} ???return null;}/** * 带有参数的POST * @param url 请求地址 * @param params 请求参数 * @return 返回状态码为200 HttpResut(200,内容) 否则,返回HttpResut(当前状态码,null) * @throws Exception */public HttpResut dopost(String url, Map<String, Object> params) throws Exception { ???// 创建http POST请求 ???HttpPost httpPost = new HttpPost(url); ???// 设置2个post参数,一个是scope、一个是q ???List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); ???if (!params.isEmpty()) { ???????for (Map.Entry<String, Object> entry : params.entrySet()) { ???????????parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); ???????} ???} ???// 构造一个form表单式的实体 ???UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8"); ???// 将请求实体设置到httpPost对象中 ???httpPost.setEntity(formEntity); ???// 设置请求参数 ???httpPost.setConfig(config); ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpPost); ???????// 判断返回状态是否为200 ???????if (response.getStatusLine().getStatusCode() == 200) { ???????????return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString( ???????????????????response.getEntity(), "UTF-8")); ???????} ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???????// httpclient.close(); ???} ???return new HttpResut(response.getStatusLine().getStatusCode(), null);}/** * 没有带参数POST请求 * @throws Exception ?*/public HttpResut dopost(String url) throws Exception{ ???return this.dopost(url, new HashMap<String, Object>());}//doput dodelete dopostjson .../** * 指定POST请求 * ?* @param url * @param param 请求参数 * @return 状态码和请求的body * @throws IOException */public HttpResut doPostJson(String url, String json) throws IOException { ???// 创建http POST请求 ???HttpPost httpPost = new HttpPost(url); ???httpPost.setConfig(this.config); ???if (json != null) { ???????// 构造一个字符串的实体 ???????StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON); ???????// 将请求实体设置到httpPost对象中 ???????httpPost.setEntity(stringEntity); ???} ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpPost); ???????if (response.getEntity() != null) { ???????????return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString( ???????????????????response.getEntity(), "UTF-8")); ???????} ???????return new HttpResut(response.getStatusLine().getStatusCode(), null); ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???}}/** * 执行PUT请求 * ?* @param url * @param param 请求参数 * @return 状态码和请求的body * @throws IOException */public HttpResut doPut(String url, Map<String, Object> param) throws IOException { ???// 创建http POST请求 ???HttpPut httpPut = new HttpPut(url); ???httpPut.setConfig(config); ???if (param != null) { ???????// 设置post参数 ???????List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); ???????for (Map.Entry<String, Object> entry : param.entrySet()) { ???????????parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); ???????} ???????// 构造一个form表单式的实体,并且指定参数的编码为UTF-8 ???????UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8"); ???????// 将请求实体设置到httpPost对象中 ???????httpPut.setEntity(formEntity); ???} ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpPut); ???????if (response.getEntity() != null) { ???????????return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString( ???????????????????response.getEntity(), "UTF-8")); ???????} ???????return new HttpResut(response.getStatusLine().getStatusCode(), null); ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???}}/** * 执行PUT请求 * ?* @param url * @return 状态码和请求的body * @throws IOException */public HttpResut doPut(String url) throws IOException { ???return this.doPut(url, null);}/** * 执行DELETE请求,通过POST提交,_method指定真正的请求方法 * ?* @param url * @param param 请求参数 * @return 状态码和请求的body * @throws IOException */public HttpResut doDelete(String url, Map<String, Object> param) throws Exception { ???param.put("_method", "DELETE"); ???return this.dopost(url, param);}/** * 执行DELETE请求(真正的DELETE请求) * ?* @param url * @return 状态码和请求的body * @throws IOException */public HttpResut doDelete(String url) throws Exception { ???// 创建http DELETE请求 ???HttpDelete httpDelete = new HttpDelete(url); ???httpDelete.setConfig(this.config); ???CloseableHttpResponse response = null; ???try { ???????// 执行请求 ???????response = httpClient.execute(httpDelete); ???????if (response.getEntity() != null) { ???????????return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString( ???????????????????response.getEntity(), "UTF-8")); ???????} ???????return new HttpResut(response.getStatusLine().getStatusCode(), null); ???} finally { ???????if (response != null) { ???????????response.close(); ???????} ???}}}

HttpClientUtils

原文地址:https://www.cnblogs.com/sidekick/p/8343780.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved