WebPostUtil

package com.xxx.yzks.util;

import com.xxx.yzks.domain.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author: dongff
 * @description:
 * @date: Created in 2019/4/18 13:46
 * @modified By:
 */
@Component
public class WebPostUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebPostUtil.class);


    public static void main1(String[] args) throws Exception {
        String url = "http://xxx/";
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("item", "91");
        WebPostUtil xxxUtil = new WebPostUtil();
        String xml = xxxUtil.sendPost(url, postParameters, "uuid");
        xml = xml.replace("<Result xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\">", "<Result>");
        Result result = XmlUtils.unMarshal(Result.class, xml);
        System.out.println(JsonUtil.obj2json(result));
    }

    /**
     * 2.2    投诉信息推送接口测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String url = "http://xxx/YouZhengService.asmx/createComplainInfo";
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("appeal_no", "1");
        postParameters.add("user_name", "张三");
        postParameters.add("phone", "150****1452");
        postParameters.add("email", "15354145@mail.com");
        postParameters.add("province", "北京省");
        postParameters.add("city", "北京市");
        postParameters.add("type", "1");
        postParameters.add("cause", "1");
        postParameters.add("item_no", "");
        postParameters.add("description", "投递超时,客户请求投递");
        postParameters.add("date_time", "2020-12-03");
        WebPostUtil xxxUtil = new WebPostUtil();
        String xml = xxxUtil.sendPost(url, postParameters, "uuid");
        xml = xml.replace("<Result xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\">", "<Result>");

        Result result = XmlUtils.unMarshal(Result.class, xml);
        System.out.println(JsonUtil.obj2json(result));
    }


    public String sendPost(String url, MultiValueMap<String, Object> postParameters, String uuid) throws Exception {
        String respStr = "";
        try {
            long t1 = System.currentTimeMillis();
            /*post请求需要设置请求头*/
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/x-www-form-urlencoded");
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(postParameters, headers);
            RestTemplate restTemplate = new RestTemplate();
            LOGGER.info("{}xxx客诉服务传入参数为:{}", uuid, postParameters);
            respStr = restTemplate.postForObject(url, request, String.class);
            long t2 = System.currentTimeMillis();
            LOGGER.info("{}xxx客诉服务返回结果为:{} ,耗时{}", uuid, respStr, (t2 - t1));
        } catch (Exception e) {
            LOGGER.error("{} 向xxx客诉发送POST请求出现异常!{}", uuid, e);
            throw new Exception("向xxx客诉发送POST请求异常");
        }
        return respStr;
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值