| | |
| | | package com.highdatas.mdm.util; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | 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.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author kimi |
| | |
| | | * @date 2019-12-13 13:35 |
| | | */ |
| | | |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class HttpUtils { |
| | | |
| | | |
| | | public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> params) { |
| | | SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| | | requestFactory.setConnectTimeout(15*1000); |
| | | requestFactory.setReadTimeout(15*1000); |
| | | RestTemplate client = new RestTemplate(requestFactory); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers); |
| | | // 执行HTTP请求 |
| | | ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); |
| | | return response.getBody(); |
| | | public static String HttpRestClient(String url, HttpMethod method, Map<String, String> formParams) { |
| | | return HttpRestClient(url,method,formParams, null, null); |
| | | } |
| | | |
| | | public static String HttpRestClient(String url, HttpMethod method, Map<String, String> formParams, String getParams) { |
| | | return HttpRestClient(url,method,formParams, getParams, null); |
| | | } |
| | | public static String HttpRestClient(String url, HttpMethod method, Map<String, String> formParams, MediaType mediaType) { |
| | | return HttpRestClient(url,method,formParams, null, mediaType); |
| | | } |
| | | |
| | | public static String HttpRestClient(String url, HttpMethod method, Map<String, String> formParams, String getParams, MediaType mediaType) { |
| | | if (!StringUtils.isEmpty(getParams)) { |
| | | url = url + Constant.QUESTION + getParams; |
| | | } |
| | | SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| | | requestFactory.setConnectTimeout(10*1000); |
| | | requestFactory.setReadTimeout(10*1000); |
| | | RestTemplate client = new RestTemplate(requestFactory); |
| | | client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | HttpEntity requestEntity; |
| | | if (mediaType == null){ |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | MultiValueMap<String, String> vals = new LinkedMultiValueMap(); |
| | | |
| | | Set<String> keySet = formParams.keySet(); |
| | | for (String key : keySet) { |
| | | String s = formParams.get(key); |
| | | List<String> strings = new ArrayList<>(); |
| | | strings.add(s); |
| | | vals.put(key,strings); |
| | | } |
| | | |
| | | requestEntity = new HttpEntity<MultiValueMap<String, String>>(vals, headers); |
| | | }else { |
| | | headers.setContentType(mediaType); |
| | | requestEntity = new HttpEntity<Map<String, String>>(formParams, headers); |
| | | } |
| | | |
| | | // 执行HTTP请求 |
| | | ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); |
| | | |
| | | String body = response.getBody(); |
| | | log.info(MessageFormat.format("请求外部接口:url:{0}, 返回结果:{1}", url, body)); |
| | | return body; |
| | | } |
| | | |
| | | public static String HttpRestClientByObjectParams(String url, HttpMethod method, Map<String, Object> formParams, String getParams,Map<String,String> headerValMap, MediaType mediaType) { |
| | | if (!StringUtils.isEmpty(getParams)) { |
| | | url = url + Constant.QUESTION + getParams; |
| | | } |
| | | SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| | | requestFactory.setConnectTimeout(10*1000); |
| | | requestFactory.setReadTimeout(10*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); |
| | | } |
| | | if (headerValMap != null) { |
| | | headers.setAll(headerValMap); |
| | | } |
| | | HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(formParams, headers); |
| | | // 执行HTTP请求 |
| | | ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); |
| | | |
| | | String body = response.getBody(); |
| | | log.info(MessageFormat.format("请求外部接口:url:{0}, 返回结果:{1}", url, body)); |
| | | return body; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |