当前位置:   article > 正文

request设置请求头_【SpringBoot WEB 系列】RestTemplate 之自定义请求头

request设置请求头

f6646ad4aaff57811fc17f93f3a832d5.png
【WEB 系列】RestTemplate 之自定义请求头

上一篇介绍了 RestTemplate 的基本使用姿势,在文末提出了一些扩展的高级使用姿势,本篇将主要集中在如何携带自定义的请求头,如设置 User-Agent,携带 Cookie

  • Get 携带请求头
  • Post 携带请求头
  • 拦截器方式设置统一请求头

I. 项目搭建

1. 配置

借助 SpringBoot 搭建一个 SpringWEB 项目,提供一些用于测试的 REST 服务

  • SpringBoot 版本: 2.2.1.RELEASE
  • 核心依赖: spring-boot-stater-web
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. </dependencies>

为了后续输出的日志更直观,这里设置了一下日志输出格式,在配置文件application.yml中,添加

  1. logging:
  2. pattern:
  3. console: (%msg%n%n){blue}

2. Rest 服务

添加三个接口,分别提供 GET 请求,POST 表单,POST json 对象,然后返回请求头、请求参数、cookie,具体实现逻辑相对简单,也不属于本篇重点,因此不赘述说明

  1. @RestController
  2. public class DemoRest {
  3. private String getHeaders(HttpServletRequest request) {
  4. Enumeration<String> headerNames = request.getHeaderNames();
  5. String name;
  6. JSONObject headers = new JSONObject();
  7. while (headerNames.hasMoreElements()) {
  8. name = headerNames.nextElement();
  9. headers.put(name, request.getHeader(name));
  10. }
  11. return headers.toJSONString();
  12. }
  13. private String getParams(HttpServletRequest request) {
  14. return JSONObject.toJSONString(request.getParameterMap());
  15. }
  16. private String getCookies(HttpServletRequest request) {
  17. Cookie[] cookies = request.getCookies();
  18. if (cookies == null || cookies.length == 0) {
  19. return "";
  20. }
  21. JSONObject ck = new JSONObject();
  22. for (Cookie cookie : cookies) {
  23. ck.put(cookie.getName(), cookie.getValue());
  24. }
  25. return ck.toJSONString();
  26. }
  27. private String buildResult(HttpServletRequest request) {
  28. return buildResult(request, null);
  29. }
  30. private String buildResult(HttpServletRequest request, Object obj) {
  31. String params = getParams(request);
  32. String headers = getHeaders(request);
  33. String cookies = getCookies(request);
  34. if (obj != null) {
  35. params += " | " + obj;
  36. }
  37. return "params: " + params + "nheaders: " + headers + "ncookies: " + cookies;
  38. }
  39. @GetMapping(path = "get")
  40. public String get(HttpServletRequest request) {
  41. return buildResult(request);
  42. }
  43. @PostMapping(path = "post")
  44. public String post(HttpServletRequest request) {
  45. return buildResult(request);
  46. }
  47. @Data
  48. @NoArgsConstructor
  49. public static class ReqBody implements Serializable {
  50. private static final long serialVersionUID = -4536744669004135021L;
  51. private String name;
  52. private Integer age;
  53. }
  54. @PostMapping(path = "body")
  55. public String postBody(@RequestBody ReqBody body) {
  56. HttpServletRequest request =
  57. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  58. return buildResult(request, body);
  59. }
  60. }

II. 使用姿势

最常见的携带请求头的需求,无非是 referer 校验,user-agent 的防爬以及携带 cookie,使用 RestTemplate 可以借助HttpHeaders来处理请求头

1. Get 携带请求头

前一篇博文介绍了 GET 请求的三种方式,但是getForObject/getForEntity都不满足我们的场景,这里需要引入exchange方法

  1. public void header() {
  2. RestTemplate restTemplate = new RestTemplate();
  3. HttpHeaders headers = new HttpHeaders();
  4. headers.set("user-agent",
  5. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
  6. headers.set("cookie", "my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;");
  7. // 注意几个请求参数
  8. HttpEntity<String> res = restTemplate
  9. .exchange("http://127.0.0.1:8080/get?name=一灰灰&age=20", HttpMethod.GET, new HttpEntity<>(null, headers),
  10. String.class);
  11. log.info("get with selfDefine header: {}", res);
  12. }

exchange 的使用姿势和我们前面介绍的postForEntity差不多,只是多了一个指定 HttpMethod 的参数而已

重点在于将请求头塞入 HttpEntity

输出结果

  1. (get with selfDefine header: <200,params: {"name":["一灰灰"],"age":["20"]}
  2. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  3. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"},[Content-Type:"text/plain;charset=UTF-8", Content-Length:"447", Date:"Mon, 29 Jun 2020 07:48:49 GMT"]>

2. Post 携带请求头

post 携带请求头,也可以利用上面的方式实现;当然我们一般直接借助postForObject/postForEntity就可以满足需求了

  1. // httpHeaders 和上面的一致,这里省略相关代码
  2. // post 带请求头
  3. MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
  4. params.add("name", "一灰灰Blog");
  5. params.add("age", 20);
  6. String response = restTemplate
  7. .postForObject("http://127.0.0.1:8080/post", new HttpEntity<>(params, headers), String.class);
  8. log.info("post with selfDefine header: {}", response);

输出结果

  1. (post with selfDefine header: params: {"name":["一灰灰Blog"],"age":["20"]}
  2. headers: {"content-length":"338","cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","content-type":"multipart/form-data;charset=UTF-8;boundary=2VJHo9r6lYgR_WoSBy1FQC40jvBvGtLk7QUaymGg","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  3. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"}

3. 拦截器方式

如果我们可以确定每次发起请求时,都要设置一个自定义的 User-Agent,每次都使用上面的两种姿势就有点繁琐了,因此我们是可以通过拦截器的方式来添加通用的请求头,这样使用这个 RestTemplate 时,都会携带上请求头

  1. // 借助拦截器的方式来实现塞统一的请求头
  2. ClientHttpRequestInterceptor interceptor = (httpRequest, bytes, execution) -> {
  3. httpRequest.getHeaders().set("user-agent",
  4. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
  5. httpRequest.getHeaders().set("cookie", "my_user_id=haha123; UN=1231923;gr_user_id=interceptor;");
  6. return execution.execute(httpRequest, bytes);
  7. };
  8. restTemplate.getInterceptors().add(interceptor);
  9. response = restTemplate.getForObject("http://127.0.0.1:8080/get?name=一灰灰&age=20", String.class);
  10. log.info("get with selfDefine header by Interceptor: {}", response);

上面这个使用姿势比较适用于通用的场景,测试输出

  1. (get with selfDefine header by Interceptor: params: {"name":["一灰灰"],"age":["20"]}
  2. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=interceptor;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  3. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"interceptor"}

4. 请求头错误使用姿势

在我们使用自定义请求头时,有一个需要特殊重视的地方,HttpHeaders 使用不当,可能导致请求头爆炸

  1. /**
  2. * 错误的请求头使用姿势
  3. */
  4. public void errorHeader() {
  5. RestTemplate restTemplate = new RestTemplate();
  6. int i = 0;
  7. // 为了复用headers,避免每次都创建这个对象,但是在循环中又是通过 add 方式添加请求头,那么请求头会越来越膨胀,最终导致请求超限
  8. // 这种case,要么将add改为set;要么不要在循环中这么干
  9. HttpHeaders headers = new HttpHeaders();
  10. while (++i < 5) {
  11. headers.add("user-agent",
  12. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
  13. headers.add("cookie", "my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;");
  14. HttpEntity<String> res = restTemplate.exchange("http://127.0.0.1:8080/get?name=一灰灰&age=20", HttpMethod.GET,
  15. new HttpEntity<>(null, headers), String.class);
  16. log.info("get with selfDefine header: {}", res);
  17. }
  18. }

上面演示的关键点为

  • 希望复用 HttpHeaders
  • headers.add 方式添加请求头;而不是前面的 set方式

输出如下,请注意每一次请求过后,请求头膨胀了一次

  1. (get with selfDefine header: <200,params: {"name":["一灰灰"],"age":["20"]}
  2. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  3. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"},[Content-Type:"text/plain;charset=UTF-8", Content-Length:"447", Date:"Mon, 29 Jun 2020 07:48:49 GMT"]>
  4. (get with selfDefine header: <200,params: {"name":["一灰灰"],"age":["20"]}
  5. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  6. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"},[Content-Type:"text/plain;charset=UTF-8", Content-Length:"503", Date:"Mon, 29 Jun 2020 07:48:49 GMT"]>
  7. (get with selfDefine header: <200,params: {"name":["一灰灰"],"age":["20"]}
  8. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  9. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"},[Content-Type:"text/plain;charset=UTF-8", Content-Length:"559", Date:"Mon, 29 Jun 2020 07:48:49 GMT"]>
  10. (get with selfDefine header: <200,params: {"name":["一灰灰"],"age":["20"]}
  11. headers: {"cookie":"my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;; my_user_id=haha123; UN=1231923;gr_user_id=welcome_yhh;","host":"127.0.0.1:8080","connection":"keep-alive","accept":"text/plain, application/json, application/*+json, */*","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
  12. cookies: {"my_user_id":"haha123","UN":"1231923","gr_user_id":"welcome_yhh"},[Content-Type:"text/plain;charset=UTF-8", Content-Length:"615", Date:"Mon, 29 Jun 2020 07:48:49 GMT"]>

II. 其他

0. 项目&系列博文

系列博文

  • 【WEB 系列】RestTemplate 基础用法小结

源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo
  • 项目: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/221-web-resttemplate

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰 Blog 个人博客 https://blog.hhui.top
  • 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top

ced9023adbf1402e483cd585098b2c71.png
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/924313
推荐阅读
相关标签
  

闽ICP备14008679号