package com.scm.contract.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author zuo
 * @Version 1.0
 * @Date 2021/4/14 17:25
 */
public class RestTemplateUtils {

    public static Map postSend(String url, Object data) {
        Map map = null;
        try {
            String params = JSON.toJSONString(data);
            //复杂构造函数的使用
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(30000);// 设置超时
            requestFactory.setReadTimeout(30000);
            //建RestTemplate
            RestTemplate client = new RestTemplate(requestFactory);
            HttpHeaders headers = new HttpHeaders();
            HttpMethod method = HttpMethod.POST;
            // 以表单的方式提交
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            //将请求头部和参数合成一个请求
            HttpEntity<String> stringHttpEntity = new HttpEntity<>(params, headers);
            //执行HTTP请求，将返回的结构使用ResultVO类格式化
            ResponseEntity<String> exchange = client.exchange(url, method, stringHttpEntity, String.class);
            String body = exchange.getBody();
            map = JSON.parseObject(body);
        } catch (Exception ignored) {
        }
        return map;
    }
}
