From d0451fdd55195901e65e5c4b3b64028a86f9e669 Mon Sep 17 00:00:00 2001 From: kimi42345 <kimi42345@outlook.com> Date: 星期日, 22 三月 2020 22:10:54 +0800 Subject: [PATCH] no message --- src/main/java/com/highdatas/mdm/util/HttpUtils.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/highdatas/mdm/util/HttpUtils.java b/src/main/java/com/highdatas/mdm/util/HttpUtils.java index 413b6c4..e8960e6 100644 --- a/src/main/java/com/highdatas/mdm/util/HttpUtils.java +++ b/src/main/java/com/highdatas/mdm/util/HttpUtils.java @@ -1,9 +1,18 @@ package com.highdatas.mdm.util; + import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * @author kimi @@ -11,20 +20,60 @@ * @date 2019-12-13 13:35 */ - +@Component public class HttpUtils { + public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> formParams) { + return HttpRestClient(url,method,formParams, null, null); + } - public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> params) { + public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> formParams, String getParams) { + return HttpRestClient(url,method,formParams, getParams, null); + } + public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> formParams, MediaType mediaType) { + return HttpRestClient(url,method,formParams, null, mediaType); + } + + public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> formParams, String getParams, MediaType mediaType) { + if (!StringUtils.isEmpty(getParams)) { + url = url + Constant.QUESTION + getParams; + } SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(15*1000); requestFactory.setReadTimeout(15*1000); RestTemplate client = new RestTemplate(requestFactory); + client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); + if (mediaType == null){ + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + }else { + headers.setContentType(mediaType); + } - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers); + HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(formParams, headers); // 鎵цHTTP璇锋眰 ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); + return response.getBody(); } + + public static String getUrlParamsByMap(Map<String, Object> map) { + if (map == null) { + return ""; + } + StringBuffer sb = new StringBuffer(); + for (Map.Entry<String, Object> entry : map.entrySet()) { + Object mapValue = entry.getValue(); + if (mapValue instanceof List) { + List<Object> valueList = (List<Object>) mapValue; + mapValue = valueList.stream().map(value -> value.toString()).collect(Collectors.joining(Constant.SEMICOLON)); + } + sb.append(entry.getKey() + "=" + mapValue); + sb.append("&"); + } + String s = sb.toString(); + if (s.endsWith("&")) { + s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "&"); + } + return s; + } } -- Gitblit v1.8.0