• RestTemplate 发送Post 多个参数请求


              MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
                    requestEntity.add("clientFlag", clientFlag);
                    requestEntity.add("xml", xml);
                    requestEntity.add("verifyData", strMd5);
             String s = REST_TEMPLATE.postForObject("http://10.10.129.19/svsr/Receive.asmx/OrderXML", requestEntity, String.class);


     !!

        最直接的方法就是 写个类吧!!

            可惜了 JAVA 没有  c# 中  匿名类 这个东西啊

      var news = new { title="特大喜讯",author="夕阳眼",postdate="3013-10-9",msg="今晚公布"};

     补充:

      设置请求头:

      

            MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
            postParameters.add("userCode", "291974");
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/x-www-form-urlencoded");
            HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(postParameters, headers);
    
            String data= restTemplate.postForObject("http://10.10.12.27:9000/Criteria", r, String.class);
            System.out.println(data);
    注意:
        RestTemplate 会对请求头判断,会更具请求头不通走不同的逻辑。默认是 text/html /*
          如果是  application/x-www-form-urlencoded  这个请求头  会对数据镜像 url 编码。
        
        不可以传递 非 字符串类型的数据!!



    关于 HttpEntity 这个对象的一点说明

      
    HttpEntity  就是存放 两个字段数据  一个是请求数据  一个是请求头!  从定义上就可以看到   虽然可以 POST 等 提交from  数据  但是好是推荐使用实体类型来传递 HTTP 请求数据。
    public class HttpEntity<T> {
        private final HttpHeaders headers;
        private final T body;
        
        public HttpEntity(T body, MultiValueMap<String, String> headers) {
            this.body = body;
            HttpHeaders tempHeaders = new HttpHeaders();
            if (headers != null) {
                tempHeaders.putAll(headers);
            }
            this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
        }
    }

  • 相关阅读:
    IOS Application生命周期
    IOS ViewController 生命周期
    nsinteger 与 int 区别
    判断日期是昨天,今天,明天,后天,其他的显示星期
    改变图片颜色(灰色显示)
    Linux系统管理
    PHP 相关软件下载
    检查域名是否正确解析网站
    定时任务
    BZOJ 3534 [Sdoi2014]重建
  • 原文地址:https://www.cnblogs.com/atliwen/p/6289883.html
Copyright © 2020-2023  润新知