使用restTemplate发包

蔚落 2023-10-13 21:02 83阅读 0赞

1.发送application/x-www-form-urlencoded

  1. final String myId = id;
  2. //参数
  3. MultiValueMap<String, String> paramMap = new LinkedMultiValueMap(){
  4. {
  5. add("id", myId);
  6. add("ymbb", TaxFpdkVersionEnum.getValue(taxBureauRequest.getZoneCode()));
  7. }
  8. };
  9. //封装请求头
  10. RequestEntity requestEntity = RequestEntity
  11. .post(new URI("")) // 可以不填
  12. .contentType(MediaType.APPLICATION_FORM_URLENCODED) // 参数传输类型 url编码格式 application/x-www-form-urlencoded
  13. .accept(MediaType.ALL).acceptCharset(StandardCharsets.UTF_8)
  14. .body(paramMap);
  15. //基于注解映射对应实体
  16. ResponseEntity<String> responseEntity = restTemplate.postForEntity(urllogin, requestEntity, String.class);
  17. result = responseEntity.getBody();

2.发送get请求

  1. HttpHeaders headers = new HttpHeaders();
  2. if(oauthloginUrl.startsWith("https://etax") || oauthloginUrl.startsWith("http://etax")){//这个cookie是电子税局的,设置时需要注意
  3. headers.set("Cookie", taxBureauRequest.getCookie());
  4. headers.set("Host", "etax.xinjiang.chinatax.gov.cn");
  5. headers.set("User-Agent", this.userAgent);
  6. }else {
  7. headers.set("Host", "fpdk.xinjiang.chinatax.gov.cn");
  8. headers.set("User-Agent", this.userAgent);
  9. }
  10. HttpEntity<JSONObject> httpEntity = new HttpEntity<>( headers);
  11. ResponseEntity<String> responseEntity = restTemplate.exchange(oauthloginUrl, HttpMethod.GET, httpEntity, String.class);
  12. log.info("xinjiang getToken oauthaccessUrl start:"+oauthloginUrl);
  13. responseCode = responseEntity.getStatusCode().value();
  14. result = responseEntity.getBody();

如有问题,请私信。

  1. tL+8vMr1vbvB96Osx+vO8NPD09q3x7eo08PNvqGjDQoNCsTmz/LP4LnYv86zzKGiSlOyubu3vrO/zrPMoaJKQVZBz+C52L/Os8zI59Do0qrSsr/J0tTBqs+1UVGhow0KDQrX99XfIFFRIDQwNDU0MDIyOQ==

发表评论

表情:
评论列表 (有 0 条评论,83人围观)

还没有评论,来说两句吧...

相关阅读

    相关 restTemplate使用

    目录 目录 一、概述? 二、使用步骤 1.引入依赖 2.创建RestTemplate对象,交由spring容器进行管理 3.使用方法 3.1 GET请求