HTTP请求协议

[复制链接]
admin 发表于 7 小时前 | 显示全部楼层 |阅读模式
请求头

含义

Host

表示请求的主机名
User-Agent

浏览器版本。 例如:Chrome浏览器的标识类似Mozilla/5.0 ...Chrome/79 ,IE浏览器的标识类似Mozilla/5.0 (Windows NT ...)like Gecko
Accept

表示浏览器能接收的资源类型,如text/*,image/*或者*/*表示所有;
Accept-Language

表示浏览器偏好的语言,服务器可以据此返回不同语言的网页;
Accept-Encoding

表示浏览器可以支持的压缩类型,例如gzip, deflate等。
Content-Type

请求主体的数据类型
Content-Length

数据主体的大小(单位:字节)

  1. package com.jinhei;

  2. import jakarta.servlet.http.HttpServletRequest;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;

  5. @RestController
  6. public class RequestController {
  7.     /**
  8.      * 请求路径 http://localhost:8080/request?name=Tom&age=18
  9.      * @param request
  10.      * @return
  11.      */
  12.     @RequestMapping("/request")
  13.     public String request(HttpServletRequest request){
  14.         //1.获取请求参数 name, age
  15.         String name = request.getParameter("name");
  16.         String age = request.getParameter("age");
  17.         System.out.println("name = " + name + ", age = " + age);

  18.         //2.获取请求路径
  19.         String uri = request.getRequestURI();
  20.         String url = request.getRequestURL().toString();
  21.         System.out.println("uri = " + uri);
  22.         System.out.println("url = " + url);

  23.         //3.获取请求方式
  24.         String method = request.getMethod();
  25.         System.out.println("method = " + method);

  26.         //4.获取请求头
  27.         String header = request.getHeader("User-Agent");
  28.         System.out.println("header = " + header);
  29.         return "request success";
  30.     }
  31. }
复制代码
1.jpg

  • HTTP响应协议

  1. package com.jinhei;

  2. import jakarta.servlet.http.HttpServletResponse;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;

  6. import java.io.IOException;

  7. @RestController
  8. public class ResponseController {

  9.     @RequestMapping("/response")
  10.     public void response(HttpServletResponse response) throws IOException {
  11.         //1.设置响应状态码
  12.         response.setStatus(401);
  13.         //2.设置响应头
  14.         response.setHeader("name","itcast");
  15.         //3.设置响应体
  16.         response.setContentType("text/html;charset=utf-8");
  17.         response.setCharacterEncoding("utf-8");
  18.         response.getWriter().write("<h1>hello response</h1>");
  19.     }

  20.     @RequestMapping("/response2")
  21.     public ResponseEntity<String> response2(HttpServletResponse response) throws IOException {
  22.         return ResponseEntity
  23.                 .status(401)
  24.                 .header("name","itcast")
  25.                 .body("<h1>hello response</h1>");
  26.     }

  27. }
复制代码


网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表