package com.hxw.util;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
public class HttpClientUtil {
private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
public static String doGet(String url, Map<String, String> map) {
return doGet(url, map, "UTF-8");
}
/**
* post请求,传参放在url上,以?key=value形式传参
*
* @param url
* @param map
* @return
*/
public static String doPost(String url, Map<String, String> map) {
return doPost(url, map, "UTF-8");
}
public static String doPost(String url, JSONObject params) {
return doPost(url, params, "UTF-8");
}
public static String doPostByGBK(String url, Map<String, String> map) {
return doPost(url, map, "GBK");
}
public static String doPostByUTF(String url, Map<String, String> map) {
return doPostHttps(url, map, "UTF-8");
}
/**
* HttpClient post请求,已body中json格式传参
*
* @param josn
* @param url
* @return
*/
public static String catPost(String josn, String url) {
//默认长连接
String content = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity postingString = new StringEntity(josn, Charsets.UTF_8);
post.setEntity(postingString);
post.setHeader("Content-Type", "application/json;charset=UTF-8");
HttpResponse response = httpClient.execute(post);
content = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
log.error("ClientProtocolException", e.getMessage());
} catch (IOException e) {
log.error("IOException", e.getMessage());
}
return content;
}
/**
* HttpURLConnection 以http方式发送请求,并将请求响应内容以String格式返回
*
* @param path 请求路径
* @param method 请求方法
* @param body 请求数据
* @return 返回响应的字符串
*/
public static String postTest(String path, String method, String body) {
String response = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
//设置连接主机超时60s
conn.setConnectTimeout(60000);
//设置从主机读取数据超时60s
conn.setReadTimeout(60000);
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
if (null != body) {
//连接
conn.connect();
OutputStream outputStream = conn.getOutputStream();
outputStream.write(body.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
}
if (conn.getResponseCode() == HttpStatus.SC_OK) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
if (null != inputStream) {
inputStreamReader = new InputStreamReader(
inputStream, "UTF-8");
bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
response = buffer.toString();
}
} catch (Exception e) {
log.error("Exception", e.getMessage());
} finally {
if (conn != null) {
conn.disconnect();
}
try {
if (null != bufferedReader) {
bufferedReader.close();
}
if (null != inputStreamReader) {
inputStreamReader.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
log.info("error", e.getMessage());
}
}
return response;
}
public static String doPost(String url, Map<String, String> map, String code) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Connection", "close");
//超时设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
httppost.setConfig(requestConfig);
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String name = it.next();
params.add(new BasicNameValuePair(name, map.get(name)));
}
String str = null;
try {
httppost.setEntity(new UrlEncodedFormEntity(params, code));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
str = EntityUtils.toString(entity, code);
}
}
} catch (UnsupportedEncodingException e) {
log.error("UnsupportedEncodingException", e.getMessage());
} catch (ClientProtocolException e) {
log.error("ClientProtocolException", e.getMessage());
} catch (IOException e) {
log.error("IOException", e.getMessage());
} finally {
try {
httppost.releaseConnection();
httpclient.close();
} catch (IOException e) {
log.error("IOException", e.getMessage());
}
}
return str;
}
/**
* 气候云AOS获取病虫害数量专用(请求头增加)
*
* @param url
* @param map
* @param token
* @return
*/
public static String doPestCountGet(String url, Map<String, String> map, String token) {
CloseableHttpClient httpclient = HttpClients.crea
AJAX+echarts+springboot实现前端向后端请求数据
需积分: 5 18 浏览量
2022-09-26
00:23:03
上传
评论
收藏 42KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/6e21e79fdec94fe082672fdc76b94e37_jeremyxun.jpg!1)
JeremyXun
- 粉丝: 0
- 资源: 2
最新资源
- 优先编码器除法电微分运算电路 全加器函数发生电路等电路经典Multisim仿真实验源文件合集(25个).zip
- 2331308JS课堂案例.zip
- STM32H750VBT6单片机最小系统开发板AD设计硬件(原理图+PCB+3D封装库)工程文件.zip
- 基于74LS161+ 74LS192芯片实现倒计时定时器Multisim仿真源文件,Multisim10以上版本可打开运行
- 科大讯飞语音引擎 jar包 demo,科大讯飞语音合成引擎3.0,支持4.0系统以上,文字转语音输出.zip
- Java架构面试笔试专题资料及经验(含答案)SpringBoot面试Linux面试专题及答案 合集.zip
- 头歌c语言实验答案tion-model-for-ne开发笔记
- docker配置使用-model-for-networK开发demo
- docker配置使用vaWeb-mas笔记
- c语言连接两个字符串-mas开发笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0