whyclxw
2024-09-21 197f1e7c794a06ee2443a7697248aa3d77fb0e39
src/main/java/com/whyc/util/TestparamHttpUtil.java
@@ -1,23 +1,77 @@
package com.whyc.util;
import com.whyc.dto.ResultA200Dto;
import com.whyc.dto.ResultActmDto;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
public class TestparamHttpUtil {
    //post请求传入form-data 格式
    public static Object postforform_data( RestTemplate restTemplate,String url,  MultiValueMap<String, Object> paramMap){
    public static Object postforform_dataA200( RestTemplate restTemplate,String url,  MultiValueMap<String, Object> paramMap){
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 设置以表单的方式提交
        headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
        HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class );
        String results= response.getBody();
        String results= null;
        try {
            ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class );
            results = response.getBody();
        } catch (RestClientException  e) {
            results=" {\n" +
                    "      \"code\": \"210\",\n" +
                    "      \"msg\": \"failed to controll\",\n" +
                    "      \"data\": null,\n" +
                    "      \"count\": 0\n" +
                    "    }";
        }
        if(results==null){
            results=" {\n" +
                    "      \"code\": \"0\",\n" +
                    "      \"msg\": \"failed to controll\",\n" +
                    "      \"data\": null,\n" +
                    "      \"count\": 0\n" +
                    "    }";
        }
        Object dto= ActionUtil.getGson().fromJson(results, ResultA200Dto.class);
        return dto;
    }
    //post请求传入form-data 格式
    public static Object postforform_dataActm( RestTemplate restTemplate,String url,  MultiValueMap<String, Object> paramMap){
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 设置以表单的方式提交
        headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
        HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers);
        String results= null;
        try {
            ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class );
            results = response.getBody();
        } catch (RestClientException e) {
            results=" {\n" +
                    "      \"code\": \"210\",\n" +
                    "      \"msg\": \"failed to controll\",\n" +
                    "      \"data\": null,\n" +
                    "      \"count\": 0\n" +
                    "    }";
        }
        if(results==null){
            results=" {\n" +
                    "      \"code\": \"0\",\n" +
                    "      \"msg\": \"failed to controll\",\n" +
                    "      \"data\": null,\n" +
                    "      \"count\": 0\n" +
                    "    }";
        }
        Object dto= ActionUtil.getGson().fromJson(results, ResultActmDto.class);
        return dto;
    }
}