zoukankan      html  css  js  c++  java
  • restTemplate使用实例

    请求携带cookie

            HttpHeaders headers = new HttpHeaders();
            List<String> cookies = new ArrayList<>();
            cookies.add("JSESSIONID=" + Strings.nullToEmpty(jsessionId));
            cookies.add("token=" + Strings.nullToEmpty(token));
            headers.put(HttpHeaders.COOKIE,cookies);
            HttpEntity request = new HttpEntity(null, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
    

    post表单

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
            map.add("title", title);
            map.add("desc", desc);
            map.add("userid", toUserId);
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
    

    post json

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
            HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
            ResponseEntity<String> resp = restTemplate.postForEntity(url,entity,String.class);
    

    url post

            String template = baseUrl + "/demo?app={0}&userId={1}";
            String url = MessageFormat.format(template,app,userId);
            return restTemplate.postForEntity(url,null,String.class);
    

    请求图片

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<byte[]> response = restTemplate.exchange(url,HttpMethod.GET, entity, byte[].class);
    byte[] imageBytes = response.getBody();
    
  • 相关阅读:
    截取某一个元素的图
    11、python的异常机制
    10、python面向对象三大特性:封装、继承、多态
    9、python之面向对象
    软件测试有前途吗?
    对应届生做测试的同学们的一些建议
    没有代码基础如何学习自动化测试?
    接口自动化测试有哪些工具或者框架?
    软件测试流程
    接口自动化测试中logging实际用法
  • 原文地址:https://www.cnblogs.com/braless/p/15007246.html
Copyright © 2011-2022 走看看