|
@@ -12,13 +12,21 @@ import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
|
|
|
-import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.Consts;
|
|
|
+import org.apache.http.Header;
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import org.apache.http.*;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
+import org.apache.http.client.params.AllClientPNames;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
@@ -36,6 +44,7 @@ public class HttpUtils
|
|
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
private static final String UTF8 = "UTF-8";
|
|
|
private static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded; charset=UTF-8";
|
|
|
+ private static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
|
|
/**
|
|
|
* 向指定 URL 发送GET方法的请求
|
|
@@ -417,4 +426,90 @@ public class HttpUtils
|
|
|
private static void collectAPISpendTime(String url, long startTime, long endTime) {
|
|
|
log.debug("HTTP请求耗时分析,请求URL: {} , 耗时: {} ms", url, endTime - startTime);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定 URL 发送POST方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的 URL
|
|
|
+ * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
+ * @param headersMap 请求头
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
+ */
|
|
|
+ public static CloseableHttpResponse sendPostHeader(String url, String param, Map<String, String> headersMap)
|
|
|
+ {
|
|
|
+ // 字符串编码
|
|
|
+ StringEntity entity = new StringEntity(param, Consts.UTF_8);
|
|
|
+ // 设置content-type
|
|
|
+ entity.setContentType("application/json");
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 防止被当成攻击添加的
|
|
|
+ httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1");
|
|
|
+ // 接收参数设置
|
|
|
+ httpPost.setHeader("connection", "Keep-Alive");
|
|
|
+ httpPost.setHeader("Accept", "application/json");
|
|
|
+ httpPost.setHeader("Accept-Charset", "utf-8");
|
|
|
+ httpPost.setHeader("contentType", "utf-8");
|
|
|
+ headersMap.forEach(httpPost::setHeader);
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
+ Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
+ Arrays.stream(headers).forEach(httpPost::addHeader);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String postFormBodyHeader(String url, Map<String, String> paramMap, Map<String, String> headersMap, Header[] header) throws IOException{
|
|
|
+ return postHearder(url, paramMap, null, headersMap, header);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向url发送post请求表单提交数据
|
|
|
+ * @param url 请求url
|
|
|
+ * @param paramMap 表单数据
|
|
|
+ * @param encoding 编码
|
|
|
+ * @param headersMap 请求头
|
|
|
+ * @return 请求返回的数据
|
|
|
+ * @throws IOException 读写异常
|
|
|
+ */
|
|
|
+ private static String postHearder(String url, Map<String, String> paramMap, String encoding, Map<String, String> headersMap, Header[] header) throws IOException {
|
|
|
+ log.debug("http 请求 url: {} , 请求参数: {}", url, appendUrl("", paramMap).replace("?", ""));
|
|
|
+ encoding = encoding == null ? UTF8 : encoding;
|
|
|
+ // 创建post方式请求对象
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 装填参数
|
|
|
+ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
|
|
+ if (paramMap != null) {
|
|
|
+ for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
|
|
+ String value = entry.getValue();
|
|
|
+ //去掉如下判断会造成String类型的value为null时
|
|
|
+ if (value != null) {
|
|
|
+ nameValuePairs.add(new BasicNameValuePair(entry.getKey(), value));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 设置参数到请求对象中
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
|
|
|
+ // 设置header信息
|
|
|
+ // 指定报文头【Content-type】、【User-Agent】
|
|
|
+ httpPost.setHeader("Content-type", APPLICATION_FORM_URLENCODED);
|
|
|
+ httpPost.setHeader("Accept", "*/*");
|
|
|
+ httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1");
|
|
|
+ headersMap.forEach(httpPost::setHeader);
|
|
|
+ Arrays.stream(header).forEach(head->{
|
|
|
+ httpPost.addHeader("Cookie", head.getValue());
|
|
|
+ });
|
|
|
+ //Arrays.stream(header).forEach(httpPost::addHeader);
|
|
|
+ return post(url, httpPost, encoding, new DataParse<String>() {
|
|
|
+ @Override
|
|
|
+ public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
|
|
|
+ return EntityUtils.toString(httpEntity, encoding);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
}
|