Table of contents
3. Classification of status codes:
4. Complete status code summary:
3. HTTP request packet and response packet
(2) doGet returns data to the browser
(3) Request line for form submission data
(4) What are the GET requests?
(2) What are the POST requests?
1. Quick Start with HTTP
1. Introduction to HTTP:
HTTP, full name HyperTextTransferProtocol, refers to Hypertext Transfer Protocol; HTTP is a network protocol widely used on the Internet.
HTTP is an application layer protocol of the TCP/IP protocol. It works on the basis of the TCP/IP protocol (Network Transport Protocol). All WWW files comply with this standard. Among them, HTTP1.0 short connection and HTTP1.1 long connection.
2.HTTP request header:
Header
|
explain | Example |
---|---|---|
Accept
|
Specify the content types the client can receive | Accept: text/plain, text/html |
Accept-Charset
|
The character encoding set that the browser can accept. | Accept-Charset: iso-8859-5 |
Accept-Encoding
|
Specifies the compression encoding type (compression algorithm) of content returned by the web server that the browser can support. |
Accept-Encoding: compress, gzip
|
Accept-Language
|
Languages accepted by the browser | Accept-Language: en,zh |
Accept-Ranges
|
You can request one or more sub-range fields of the web page entity | Accept-Ranges: bytes |
Authorization
|
Authorization certificate for HTTP authorization | Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
Cache-Control
|
Specify the caching mechanism that requests and responses follow |
Cache-Control: no-cache
|
Connection
|
Indicates whether a persistent connection is required. (HTTP 1.1 makes persistent connections by default)
|
Connection: close (keep
-alive means long connection)
|
Cookie
|
When an HTTP request is sent, all cookie values stored under the requested domain name will be sent to the web server.
|
Cookie: $Version=1; Skin=new; |
Content-Length
|
Requested content length | Content-Length: 348 |
Content-Type
|
The requested MIME information corresponding to the entity |
Content-Type: application/x-www-form-urlencoded
|
Date
|
The date and time the request was sent |
Date: Tue, 15 Nov 2010 08:12:31 GMT
|
Expect
|
Request specific server behavior | Expect: 100-continue |
From
|
Email of the user who made the request | From: [email protected] |
Host
|
Specify the domain name of the requested server (the domain name is a mapping of IP, sometimes also called the host name) and port number; | Host: www.zcmhi.com |
If-Match
|
Valid only if the request content matches the entity | If-Match: “737060cd8c284d8af7ad3082f209582d” |
If-Modified-Since
|
If the requested part is modified after the specified time, the request is successful. If it is not modified, a 304 code is returned ( provided that the browser does not disable caching ) |
If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT
(
①Tell
the server that I have the resource;
②Send
the most recent modification time of the resource to the server)
After receiving the request, the server will compare the latest modification time of the resource. If the server's resource has been updated, the updated resource will be returned to the browser; if the server's resource has not been updated, it will return a 304 status code without returning the requested resource. resources .
This is the real reason why modifications to static pages do not require re-publishing the web application, the fundamental reason . (HTTP request mechanism) |
If-None-Match
|
If the content has not changed, a 304 code is returned. The parameter is the Etag previously sent by the server. Compare it with the Etag responded by the server to determine whether it has changed.
|
If-None-Match:
“737060cd8c284d8af7ad3082f209582d”
|
If-Range
|
If the entity has not changed, the server sends the client the missing parts, otherwise the entire entity is sent. The parameter is also Etag
|
If-Range: “737060cd8c284d8af7ad3082f209582d” |
If-Unmodified-Since
|
The request succeeds only if the entity has not been modified after the specified time | If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT |
Max-Forwards
|
Limit the time that information travels through proxies and gateways | Max-Forwards: 10 |
Pragma
|
Used to contain implementation-specific instructions | Pragma: no-cache |
Proxy-Authorization
|
连接到代理的授权证书 |
Proxy-Authorization: Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==
|
Range
|
只请求实体的一部分,指定范围 | Range: bytes=500-999 |
Referer
|
先前网页的地址,当前请求网页紧随其后,即请求的来路,发起者。(可用于防盗链) | Referer: http://www.zcmhi.com/archives/71.html |
TE
|
客户端愿意接受的传输编码,并通知服务器接受接受尾加头信息 | TE: trailers,deflate;q=0.5 |
Upgrade
|
向服务器指定某种传输协议以便服务器进行转换(如果支持) | Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 |
User-Agent
|
User-Agent 的内容包含发出请求的用户信息 | User-Agent: Mozilla/5.0 (Linux; X11) |
Via
|
通知中间网关或代理服务器地址,通信协议 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning
|
关于消息实体的警告信息 | Warn: 199 Miscellaneous warning |
eg : (请求包)
GET /Cyan/cyan.html HTTP/1.1(GET请求第一行是请求行,请求行下面的内容是请求头)
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
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: www.cyanra9.com:8080
If-Modified-Since: Fri, 14 Jul 2023 23:28:17 GMT
If-None-Match: W/"307-1689377297994"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
3.HTTP响应头 :
Header
|
解释 | 示例 |
---|---|---|
Accept-Ranges
|
表明服务器是否支持指定范围请求及哪种类型的分段请求;以什么作接收 | Accept-Ranges: bytes(支持断点) |
Age
|
从原始服务器到代理缓存形成的估算时间(以秒计,非负) | Age: 12 |
Allow
|
对某网络资源的有效的请求行为,不允许则返回 405 | Allow: GET, HEAD |
Cache-Control
|
告诉所有的缓存机制是否可以缓存及哪种类型 | Cache-Control: no-cache |
Content-Encoding
|
web 服务器支持的返回内容压缩编码类型。 | Content-Encoding: gzip |
Content-Language
|
响应体的语言 | Content-Language:en,zh |
Content-Length
|
响应体的长度 | Content-Length: 348 |
Content-Location
|
请求资源可替代的备用的另一地址 | Content-Location: /index.htm |
Content-MD5
|
返回资源的 MD5 校验值 | Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== |
Content-Range
|
在整个返回体中本部分的字节位置 | Content-Range: bytes 21010-47021/47022 |
Content-Type
|
返回内容的 MIME 类型 | Content-Type: text/html; charset=utf-8 |
Date
|
原始服务器消息发出的时间(响应时间) | Date: Tue, 15 Nov 2010 08:12:31 GMT |
ETag
|
请求变量的实体标签的当前值(对资源的标识,类似于token) | ETag: “737060cd8c284d8af7ad3082f209582d” |
Expires
|
响应过期的日期和时间 | Expires: Thu, 01 Dec 2010 16:00:00 GMT |
Last-Modified
|
请求资源的最后修改时间 (服务器会对请求资源的Last-Modified进行对比,然后根据不同情况决定是否直接使用浏览器缓存中的资源,例如图片 [前提是浏览器没有禁用缓存] ) |
Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT |
Location
|
用来重定向接收方到非请求 URL 的位置来完成请求或标识新的资源(描述重定向的具体位置) | Location: http://www.zcmhi.com/archives/94.html |
Pragma
|
包括实现特定的指令,它可应用到响应链上的任何接收方 | Pragma: no-cache |
Proxy-Authenticate
|
它指出认证方案和可应用到代理的该 URL 上的参数 | Proxy-Authenticate: Basic |
refresh
|
应用于重定向或一个新的资源被创造,在 5 秒之后重定向(由网景提出, 被大部分浏览器支持)
|
Refresh: 5; url=
http://www.zcmhi.com/archives/94.html
|
Retry-After
|
如果实体暂时不可取,通知客户端在指定时间之后再次尝试 | Retry-After: 120 |
Server
|
web 服务器软件名称 | Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) |
Set-Cookie
|
设置 Http Cookie |
Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
|
Trailer
|
指出头域在分块传输编码的尾部存在 | Trailer: Max-Forwards |
Transfer-Encoding
|
文件传输编码 | Transfer-Encoding:chunked |
Vary
|
告诉下游代理是使用缓存响应还是从原始服务器请求 | Vary: * |
Via
|
告知代理客户端响应是通过哪里发送的 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning
|
警告实体可能存在的问题 | Warning: 199 Miscellaneous warning |
WWW-Authenticate
|
表明客户端请求实体应该使用的授权方案 | WWW-Authenticate: Basic |
eg : (响应包)
HTTP/1.1 200(HTTP响应第一行是响应行,响应行下面的内容是响应头,最后跟着的是响应体)
Accept-Ranges: bytes
ETag: W/"307-1689377316925"
Last-Modified: Fri, 14 Jul 2023 23:28:36 GMT
Content-Type: text/html(浏览器解析返回的结果并显示)
Content-Length: 307(浏览器接收到的响应体经过解析后,会加载到浏览器内核的内存中)
Date: Fri, 14 Jul 2023 23:28:38 GMT
Keep-Alive: timeout=20
Connection: keep-alive
<!DOCTYPE html>(响应体)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cyan_RA9</title>
</head>
<body>
<h1 style="border:2px cornflowerblue solid; font-family: consolas; width:150px">Cyan_RA9</h1>
<img src="TID_02.jpg" width="450px"/>
</body>
</html>
二、HTTP响应状态码
1.基本介绍 :
当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头/响应头(server header)用以响应浏览器的请求。
HTTP状态码的英文为HTTP Status Code。
2.常见状态码 :
- 200 - 请求成功
- 301 - 资源(网页等)被永久转移到其它URL
- 404 - 请求的资源(网页等)不存在
- 500 - 内部服务器错误(例如代码错误)
3.状态码的分类 :
HTTP状态码由三个十进制数字组成,第一个十进制数字定义了状态码的类型,后两个数字没有分类的作用。
HTTP状态码共分为5种类型:
4.完整状态码汇总 :
状态码 |
状态码英文名称 |
中文描述 |
---|---|---|
100 |
Continue |
继续。客户端应继续其请求 |
101 |
Switching Protocols |
切换协议。服务器根据客户端的请求切换协议。只能切换到更高级的协议,例如,切换到HTTP的新版本协议 |
200 | OK |
请求成功。一般用于GET与POST请求 |
201 | Created |
已创建。成功请求并创建了新的资源 |
202 | Accepted |
已接受。已经接受请求,但未处理完成 |
203 | Non-Authoritative Information |
非授权信息。请求成功。但返回的meta信息不在原始的服务器,而是一个副本 |
204 | No Content |
无内容。服务器成功处理,但未返回内容。在未更新网页的情况下,可确保浏览器继续显示当前文档 |
205 | Reset Content |
重置内容。服务器处理成功,用户终端(例如:浏览器)应重置文档视图。可通过此返回码清除浏览器的表单域 |
206 | Partial Content |
部分内容。服务器成功处理了部分GET请求 |
300 | Multiple Choices |
多种选择。请求的资源可包括多个位置,相应可返回一个资源特征与地址的列表用于用户终端(例如:浏览器)选择 |
301 | Moved Permanently |
永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。今后任何新的请求都应使用新的URI代替 |
302 | Found |
临时移动。与301类似。但资源只是临时被移动。客户端应继续使用原有URI(重定向) |
303 | See Other |
查看其它地址。与301类似。使用GET和POST请求查看 |
304 | Not Modified |
未修改(所请求的资源与浏览器缓存中的资源相同)。所请求的资源未修改,服务器返回此状态码时,不会返回任何资源。客户端通常会缓存访问过的资源,通过提供一个头信息指出客户端希望只返回在指定日期之后修改的资源 |
305 | Use Proxy |
使用代理。所请求的资源必须通过代理访问 |
306 | Unused |
已经被废弃的HTTP状态码 |
307 | Temporary Redirect |
临时重定向。与302类似。使用GET请求重定向 |
400 | Bad Request |
客户端请求的语法错误,服务器无法理解 |
401 | Unauthorized |
请求要求用户的身份认证 |
402 | Payment Required |
保留,将来使用 |
403 | Forbidden |
服务器理解请求客户端的请求,但是拒绝执行此请求 |
404 | Not Found |
服务器无法根据客户端的请求找到资源(网页)。通过此代码,网站设计人员可设置"您所请求的资源无法找到"的个性页面 |
405 | Method Not Allowed |
客户端请求中的方法被禁止 |
406 | Not Acceptable |
服务器无法根据客户端请求的内容特性完成请求 |
407 | Proxy Authentication Required |
请求要求代理的身份认证,与401类似,但请求者应当使用代理进行授权 |
408 | Request Time-out |
服务器等待客户端发送的请求时间过长,超时 |
409 | Conflict |
服务器完成客户端的PUT请求是可能返回此代码,服务器处理请求时发生了冲突 |
410 | Gone |
客户端请求的资源已经不存在。410不同于404,如果资源以前有现在被永久删除了可使用410代码,网站设计人员可通过301代码指定资源的新位置 |
411 | Length Required |
服务器无法处理客户端发送的不带Content-Length的请求信息 |
412 | Precondition Failed |
客户端请求信息的先决条件错误 |
413 | Request Entity Too Large |
由于请求的实体过大,服务器无法处理,因此拒绝请求。为防止客户端的连续请求,服务器可能会关闭连接。如果只是服务器暂时无法处理,则会包含一个Retry-After的响应信息 |
414 | Request-URI Too Large |
请求的URI过长(URI通常为网址),服务器无法处理 |
415 | Unsupported Media Type |
服务器无法处理请求附带的媒体格式 |
416 | Requested range not satisfiable |
客户端请求的范围无效 |
417 | Expectation Failed |
服务器无法满足Expect的请求头信息 |
500 | Internal Server Error |
服务器内部错误,无法完成请求 |
501 | Not Implemented |
服务器不支持请求的功能,无法完成请求 |
502 | Bad Gateway |
充当网关或代理的服务器,从远端服务器接收到了一个无效的请求 |
503 | Service Unavailable |
由于超载或系统维护,服务器暂时的无法处理客户端的请求。延时的长度可包含在服务器的Retry-After头信息中 |
504 | Gateway Time-out |
充当网关或代理的服务器,未及时从远端服务器获取请求 |
505 | HTTP Version not supported |
服务器不支持请求的HTTP协议的版本,无法完成处理 |
三、HTTP请求包和响应包
1.请求包分析 :
1° GET请求
(1) 说明
请求包分为请求行和请求头;请求行即请求包的第一行。
请求行用于说明请求方式、请求资源、以及请求协议和版本;如下所示 :
请求头用于说明请求的具体信息。
(2) doGet返回数据给浏览器
web.xml配置文件如下 :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>KIServlet</servlet-name>
<servlet-class>page.KIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KIServlet</servlet-name>
<url-pattern>/kyrie/irving</url-pattern>
</servlet-mapping>
</web-app>
KIServlet类代码如下 :
package page;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author : Cyan_RA9
* @version : 21.0
*/
public class KIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("KIServlet's doGet is invoked");
//输出一句话,返回给浏览器
/*
通过resp对象的getWriter方法获取对应的PrintWriter流,
PrintWriter类的print方法可以给浏览器回送数据。
注意———
Δ 必须指定回送数据的“数据编码”和“数据格式”!(中间用分号隔开)
Δ setContentType方法,必须在resp获取流资源之前使用!(否则不生效)
数据编码: charset=utf-8
数据格式: text/html (MIME类型)
*/
resp.setContentType("text/html; charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.print("<h2>Kyrie Irving</h2>");
/*
为确保数据顺利返回,可以写上flush和close方法
flush() : 表示将缓存的数据进行刷新
close() : 表示关闭流,及时地释放资源
*/
writer.flush();
writer.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
运行效果 :
(3) form表单提交数据的请求行
如下图所示 :
符合速通HTML中提到的"action + ? + name=value&name=value..."的格式。
(4) GET请求有哪些?
1.<form></form>标签(method属性可以指定GET)
2.a标签
3.link标签引入CSS(浏览器以GET请求的方式来获取资源)
4.Script标签引入js文件(同上)
5.<img/>标签引入图片(同上)
6.iframe引入html页面(不常见)
7.在浏览器地址栏中输入地址后敲回车(最常见)
(5) 什么时候使用GET请求?
1> 在前台页面展示时;
2> 需要分享和传播时;
3> 对安全性要求不高时;
4> 做数据查询时;
2° POST请求
(1) 说明
POST请求和GET请求的区别是——POST请求包的内容分为请求行,请求头,请求体三部分。
一个完整的POST请求如下 :
POST /Servlet_Demo/TTT 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
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 17(表示请求体的长度)
Content-Type: application/x-www-form-urlencoded(表示提交的表单数据格式是url编码,在服务器端会自动解码)
Cookie: JSESSIONID=6AC34017BAB5AC8B0351FE7494A32CE3; Idea-79b5541a=89d08fdf-a8ef-4a20-bc79-f3a101063e05
Host: localhost:8080
Origin: http://localhost:63342(表示请求来自哪个主机)
Pragma: no-cache
Referer: http://localhost:63342/
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-site
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/111.0.0.0 Safari/537.36 sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows"(请求头)
username=Cyan_RA9(请求体, 真正的表单数据存放在这儿)
(2) POST请求有哪些?
常见POST请求,就是form表单中method属性指定post。
(3) 什么时候使用POST请求?
1> 对安全性的要求比较高时,比如有保密数据;(登录)
2> 传输数据量相对较大时;(发帖,上传文件)
3> 进行数据的增,删,改时;
2.响应包分析 :
1° 说明
HTTP 响应分为三部分——①响应行;②响应头;③响应体;
响应行用于说明协议及协议版本,状态码及状态码描述;
响应头用于详细描述响应的相关信息;
响应体就是实际返回的内容(也可能没有返回,比如304)。
2° MIME类型
MIME是 HTTP 协议中数据类型。 MIME 的英文全称是"Multipurpose Internet Mail Extensions",指多功能 Internet邮件扩充服务。
MIME 类型的格式是 " 大类型 / 小类型 ",并与某一种文件的扩展名相对应。
在响应包的 Content-Type响应头中 就有指定MIME类型。
常见MIME类型如下 :
文件 | MIME类型 |
---|---|
超文本标记语言文本 | .html,.htm eg : text/html |
普通文本 | .txt eg : text/plain |
RTF文本 | .rtf eg : application/rtf |
GIF图像 | .gif eg : image/gif |
JPEG图像 | .jpeg,.jpg eg : image/jpeg |
au声音文件 | .au eg : audio/basic |
MIDI音乐文件 | .mid,.midi eg : audio/midi,audio/x-midi |
ReadAudio音乐文件 | .ra,.ram eg : audio/x-pn-realaudio |
MPEG文件 | .mpeg,.mpg eg : video/mpeg |
AVI文件 | .avi eg : video/x-msvideo |
GZIP文件 | .gz eg : application/x-gzip |
TAR文件 | .tar eg : application/x-tar |
四、HTTP总结
(1) 熟悉请求包和响应包的结构 (请求包又可分为GET和POST两种)。
(2) 掌握常见的请求头和响应头,尤其要明白“修改静态web资源不需要重新发布Web工程”的根本原因(Last-Modified响应头,If-Modified-Since请求头)
(3) 掌握常见的状态码(200,301,302,304,404,500等)
(4) 掌握HTTP请求包和响应包的抓包分析。