|
@@ -125,6 +125,16 @@ public class HttpUtils
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static String postFormBodyHeader(String url, Map<String, String> paramMap, Map<String, String> headersMap) throws IOException{
|
|
|
+ try{
|
|
|
+ String result = postHead(url, paramMap, null,headersMap);
|
|
|
+ return result;
|
|
|
+ }catch (Exception e){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
public static String sendPost(String url, JSONObject param)
|
|
|
{
|
|
|
HttpClient client = HttpClients.createDefault();
|
|
@@ -472,6 +482,41 @@ public class HttpUtils
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
+ private static String postHead(String url, Map<String, String> paramMap, String encoding, Map<String, String> headersMap) throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
|
+ 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);
|
|
|
+ headersMap.forEach(httpPost::setHeader);
|
|
|
+ return post(url, httpPost, encoding, new DataParse<String>() {
|
|
|
+ @Override
|
|
|
+ public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
|
|
|
+ if(Validator.isNotEmpty(httpEntity)){
|
|
|
+ return EntityUtils.toString(httpEntity, encoding);
|
|
|
+ }else{
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 向url发送post请求表单提交数据
|
|
|
* @param url 请求url
|