• 概念: 超文本传输协议,规定了浏览器和服务器数据传输的规则

例子:

  • Response

    1
    2
    3
    4
    5
    6
    HTTP/1.1 200 
    Content-Type: text/html;charset=UTF-8
    Content-Length: 10
    Date: Wed, 19 Mar 2025 08:22:49 GMT
    Keep-Alive: timeout=60
    Connection: keep-alive
  • Request

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    GET /hello?name=ith HTTP/1.1
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
    Accept-Encoding: gzip, deflate, br, zstd
    Accept-Language: zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6,ru;q=0.5,eo;q=0.4
    Cache-Control: max-age=0
    Connection: keep-alive
    Host: localhost:8080
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
    sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"
    sec-ch-ua-mobile: ?0
    sec-ch-ua-platform: "Windows"

第一行是请求行, 接下来是请求头

请求request

分为Get和Post

一般第一行是请求行,以Get或者Post开始,表示Request的类型

接下来 以 key - value的是请求头,常见的请求头:

  • Host : 全球的主机名
  • User-Agent: 浏览器版本
  • Accept: 浏览器可以接受的资源类型
  • Accept-Lauguage: 浏览器偏好于一
  • Accept-Encoding: 浏览器可以支持的压缩类型, gzip, deflate等
  • Content-Type: 请求数据类型
  • Content-Length: 请求主体的大小

对于Post来说,还有请求体,把数据放在了Post,可以请求更多的数据,而Get把数据直接放在请求行(在url后面),大小有限制

Get 和 Post的区别

  • Get 的请求数据是放在请求头
  • Post的请求数据是放在请求体

Post的安全性要高于Get, 因为Get明文把请求信息放在了请求信息里面,

请求协议

相使用HTTP要使用封装好的HttpServletRequest :

常见的方法和例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.startspringboot;  

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

@RestController
public class RequestController {

@RequestMapping("/request")
public String request(HttpServletRequest request) {
// 1. 获取请求方式
String method = request.getMethod();
System.out.println("请求方式:" + method);

//2.获取url地址
String url = request.getRequestURL().toString(); // 拿到整个url的StringBuffer对象
System.out.println("url : " + url);
String uri = request.getRequestURI(); // 拿到的是资源的访问路径
System.out.println("uri " + uri);

//3.获取请求协议
String protocol = request.getProtocol();
System.out.println("协议" + protocol);

//4.获取请求参数 - name String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name + " " + age);

//5.获取请求头 - Accept String accept = request.getHeader("Accept");
System.out.println("Accept: " + accept);

return "OK";
}
}

启动项目打开浏览器输入

1
localhost:8080/request?name=你好&age=jj

可以看到输出太输出了对应信息

1
2
3
4
5
6
请求方式:GET
url : http://localhost:8080/request
uri /request
协议HTTP/1.1
你好 jj
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

响应Response

1
2
3
4
5
6
7
8
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Data: Tie, 10 May 2024 07:51:07 GMT
Keep-Alive: timeout=60
Connection: keep-alive

[{id: 1, breadName:"阿里巴巴", companyName: "腾讯计算机系统有限公司", description: "旺旺旺"}]

第一行是响应行(协议,状态码,描述)
下面是响应头:第二行开始key:value
消息空行
最后是响应体:存放响应数据

HTTP常见状态码

HTTP状态码是应用层(HTTP协议)的响应代码,用于描述HTTP请求的结果。以下是常见的HTTP状态码分类及其含义:

1xx:信息性响应

  • 100 Continue:服务器已收到请求头,客户端可以继续发送请求体。
  • 101 Switching Protocols:服务器同意切换协议。

2xx:成功

  • 200 OK:请求成功,服务器返回了请求的数据。
  • 201 Created:请求成功,并且服务器创建了新的资源。
  • 204 No Content:请求成功,但没有返回任何内容。

3xx:重定向

  • 301 Moved Permanently:资源永久重定向到新的URL。
  • 302 Found:资源临时重定向到新的URL。
  • 304 Not Modified:资源未修改,客户端可以使用缓存。

    例子: 访问http://www.baidu.com, 打开F12,看到访问了两次,第二次发生了重定向

    4xx:客户端错误

  • 400 Bad Request:请求有语法错误或参数错误。

  • 401 Unauthorized:需要身份验证。
  • 403 Forbidden:服务器拒绝执行请求。
  • 404 Not Found:资源不存在。

5xx:服务器错误

  • 500 Internal Server Error:服务器内部错误。
  • 502 Bad Gateway:网关或代理服务器收到无效响应。
  • 503 Service Unavailable:服务器暂时超载或维护中。
Content-Type 响应内容
Content-Length 长度
Content-Encoding 该响应的压缩算法
Cache-Control 客户端如何缓存
Set-Cookie 告诉浏览器为当前页面所在的域设置cookie

使用:
HttpServletResponse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.startspringboot;  

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {

/*
*方式一 : HttpServletResponse */
@RequestMapping("/response")
public void response(HttpServletResponse response) throws IOException {
// 1.设置响应状态码
response.setStatus(401);

// 2. 设置响应头
response.setHeader("name", "cyuan");

// 3. 设置响应体
response.getWriter().write("hello sponse");
}

/*
* 方式二: Spring
* */
@RequestMapping("/Response2")
public ResponseEntity<String> response2() {
return ResponseEntity.status(401)
.header("name", "JavaWeb")
.body("<h1>hello</h1>");
} // 注意要写全三个组件不然报错
}

打开浏览器访问可以看到出现了响应数据

HTTP的工作原理

  • 客户端连接到Web服务器
  • 发送一个HTTP请求(Request)
  • 服务器接受请求返回一个HTTP响应
  • 释放链接接TCP
     如果connect 为close就主动关闭TCP链接,看客户端被动关闭, 如果是keepalive则保留一段时间,在这段时间内可以继续接受信息
    
  • 客户端解析html,显示对应内容

HTTP协议特点

  • 基于TCP协议:面向链接,安全
  • 基于请求响应模型: 一次请求一次响应
  • HTTP是无状态的协议: 对于事务的处理没有记忆能力。
    • 缺点:多次请求间不能共享数据
    • 优点: 速度快