kimi
2020-05-18 c8aee7b9bfd79cfd741d7e5692520f4f51a31a86
src/main/java/com/highdatas/mdm/util/HttpUtils.java
@@ -1,17 +1,22 @@
package com.highdatas.mdm.util;
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;
/**
@@ -21,48 +26,63 @@
 */
@Component
@Slf4j
public class HttpUtils {
    public static String HttpRestClient(String url, HttpMethod method, MultiValueMap<String, String> formParams) {
    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, MultiValueMap<String, String> formParams, String getParams) {
    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, MultiValueMap<String, String> formParams, MediaType mediaType) {
    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, MultiValueMap<String, String> formParams, String getParams,  MediaType 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(15*1000);
        requestFactory.setReadTimeout(15*1000);
        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);
        }
        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();
        String body = response.getBody();
        log.info(MessageFormat.format("请求外部接口:url:{0}, 返回结果:{1}", url, body));
        return body;
    }
    public static String HttpRestClientByObjectParams(String url, HttpMethod method, MultiValueMap<String, Object> formParams, String getParams,Map<String,String> headerValMap,  MediaType mediaType) {
    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(15*1000);
        requestFactory.setReadTimeout(15*1000);
        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();
@@ -74,11 +94,13 @@
        if (headerValMap != null) {
            headers.setAll(headerValMap);
        }
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formParams, headers);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(formParams, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
        String body = response.getBody();
        log.info(MessageFormat.format("请求外部接口:url:{0}, 返回结果:{1}", url, body));
        return body;
    }
    public static String getUrlParamsByMap(Map<String, Object> map) {