JavaWeb~http request common methods/header header fields Host, User-Agent, Cookie, Referer, Content-type/Http response parsing and status code

http request method

httpIn the protocol, the most commonly used method is the getsum postmethod.
Students who are interested in the following two methods can manually open the web page and view the specific data through the fiddler packet capture.

GET request

GETmethod is the most commonly used http method and is often used to obtain resources on a server.
Enter the URL (ie web address) in the browser, and the browser will send a GET request.
There are many ways to trigger a GET request, or to trigger an http request, which have been introduced in the previous blog, and will not be repeated here.

Features of GET request :

  • The first part of the first line is GET
  • The query string in the URL may or may not be empty
  • The header part has several key-value pair structures
  • body part is empty

POST request

POSTRequest is also a common method, generally used when submitting user data to the server.
The most common case is to log in.
Features of post request:
1. The first part of the first line is POST 2. The querystring of the URL
is generally empty, but a querystring can also be added
3. The header` part has several key-value pairs~
4. The body part is generally not Empty, but if the body is empty, it's perfectly fine.

The body part of the POST request bodyalso supports many formats.
The type of this format is Content-Typerepresented by the field in the request header.
The length of the body is represented by the Content-Length field in the request header. (The length is in bytes.)

Combined with the discussion above :
There is no essential difference between the various methods in the HTTP protocol (especially between GET and POST)! !
There are some minor differences in details:
1. Data location: GET requests store custom data in the query string (query string), and POST puts custom data in the body.
2. Semantic difference: GET is generally used to "get data", and POST is generally used to submit data.
3. Idempotency: GET requests are generally designed to be "idempotent", and POST requests are generally not required to be designed to be "idempotent"

Idempotent: A request, executed once and executed multiple times, has the same effect.
4. Cacheable: GET requests are generally cached, and POST requests generally cannot be cached

Other request methods

  • PUT method: Similar to the POST method, it is idempotent and is generally used to update data.
  • DELETE method: can delete the resource specified by the server
  • OPTIONS: Returns the request format supported by the server
  • HEAD: Similar to the GET method, except that the response body is not returned, only the response header is returned.
  • TRACE: echo the request received by the server, this method will be used for testing.
  • CONNECT: Reserved. Just understand, not much use.

Recognize request headers

The overall format of the header is a "key-value pair" structure ~
each key-value pair occupies one line, and a semicolon is used to separate the key and value.

Note: There are many types of headers, only some typical ones are introduced here.


Describes the host's address/port number ~ The
address can be a domain name or an IP


Indicates the length of the data in the body body, in bytes.
Represents the type of data format in the body.

User-Agent(UA) :
Describes the properties of the browser/operating system and some version information~

as follows:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/91.0.4472.77 Safari/537.36

Different browser versions support different data display formats. Some early browsers can only display text styles. With the development of the Internet, later browsers can gradually display pictures, audio, video and other styles.

Referer :
Indicates where the current page came from (which page jumped from)

Ads in search engines rely on this referer value to count traffic.

Cookie :
This is the most difficult field to understand in the http request header~

The value of a cookie is a string (a string defined by the programmer), and a cookie is equivalent to a mechanism for local storage on the browser side.
Cookie storage space is limited (depending on the specific implementation of the browser, generally not very large), so cookies can only save some simple information. The most typical is that the user's identity information
Cookie acts like an " identity identifier ". It is generally used for web page login. When we enter user information, the browser will send a request. At this time, the web page may automatically write a cookie, or when the server returns a response result, Set-Cookiewrite it through a field in the response header.

insert image description here

How to view web cookies

Open any webpage, click the small lock symbol in front of the protocol name, there is a cookie in use first in the webpage permission, click to view
insert image description here
insert image description here

Other ways to implement login functionality

The login functionality of the website relies on Cookie, but not entirely on, cookies.
Cookies can mainly save some information locally, and directly use the data saved by the cookie when you log in and visit next time. But there are also the following two methods to achieve similar functions:
A mechanism introduced by HTML5, browsers support a "key-to-value" method for storage. Provides a set of APIs through JS to manipulate data.
The data saved here will be stored persistently.
2. IndexDB
is a mechanism supported by relatively new browsers. The browser integrates a "database" to support SQL-like methods to operate data.
insert image description here

The function of the cookie is similar to the "visit card" when you go to the hospital to see a doctor. With this "visit card", you don't need to give your name when you go to each department, you can know your specific information directly by swiping the card. Also similar to a "passport".

Recognize the request body (body)

The body can store data in any format, but several formats are more common.
In the request header header, there is a Content-typefield, and the value after the field indicates the data format in the body body.
Common data formats are as follows:
1. application/x-www-form-urlencoded :
When the value of Content-type is the above type,
the body at this time is a key-value pair structure very similar to the query string.

When we upload an avatar on a web page, the image data will appear in the body of the body. But the picture itself is a binary data, and the http protocol is a text protocol and cannot directly transmit binary data. At this time, the picture data seen in the text is Base64 encoded for the picture content~ (Base64 algorithm is an encoding method to convert binary data into text data)
2. multipart/form-data
is mainly used to upload files ~
3. application/json
json is a very commonly used format for data organization, similar to the format for organizing objects in JS

HTTP response

Know the status code

What are HTTP status codes for? What does it do?
The HTTP status code is responsible for indicating the return result of the client's HTTP request, marking whether the server's processing is normal, notifying whether there is an error, etc. In fact, as the name suggests, it means the status of the server when the client sends a request.

Return the requested result from the server

insert image description here

Types of Status Codes

insert image description here

Specific analysis

Some common status codes are as follows:

200 ok
means that the access was successful
404 Not Found
No resource was found
403 Forbidden
Access is denied
405 Method Not Allowed
The server method is not supported
500 Internal Server Error
The server has an internal error
504 Gateway Timeout
The request timed out. (When the server is under heavy load, the time it takes for the server to process a heads-up request will increase.)
302 Move temporarily
redirects temporarily.

Similar to the "call forwarding" function of a mobile phone.

301 Moved Permanently
Permanently redirect

When the browser receives this response, subsequent requests will be automatically changed to the new address.

Status code table

status code English name Chinese description
100 Continue continue. The client should continue its request
101 Switching Protocols Switch protocols. The server switches protocols according to the client's request. Only switch to a higher level protocol, e.g. to a newer version of HTTP
200 OK The request was successful. Generally used for GET and POST requests
201 Created created. Successfully requested and created a new resource
202 Accepted accepted. The request has been accepted but not completed
203 Non-Authoritative Information Unauthorized Information. The request was successful. But the returned meta information is not on the original server, but a copy
204 No Content No content. The server processed successfully, but returned no content. Ensures the browser continues to display the current document without updating the web page
205 Reset Content Reset content. The server process is successful and the user terminal (eg: browser) should reset the document view. The browser's form fields can be cleared with this return code
206 Partial Content Part. The server successfully processed part of the GET request
300 Multiple Choices multiple choices. The requested resource can include multiple locations, and accordingly a list of resource characteristics and addresses can be returned for user terminal (eg: browser) selection
301 Moved Permanently Move permanently. The requested resource has been permanently moved to the new URI, the returned information will include the new URI, and the browser will be automatically directed to the new URI. Any new requests in the future should use the new URI instead
302 Found Temporary move. Similar to 301. But the resource is only moved temporarily. Clients should continue to use the original URI
303 See Other Check out other addresses. Similar to 301. View using GET and POST requests
304 Not Modified Unmodified. The requested resource has not been modified, and no resource will be returned when the server returns this status code. Clients typically cache visited resources, by providing a header indicating that the client wishes to return only resources modified after a specified date
305 Use Proxy Use a proxy. The requested resource must be accessed through a proxy
306 Unused Obsolete HTTP status codes
307 Temporary Redirect Temporary redirect. Similar to 302. Redirect with GET request
400 Bad Request The client request has a syntax error that the server cannot understand
401 Unauthorized Request requires user authentication
402 Payment Required reserved for future use
403 Forbidden The server understands the request of the requesting client, but refuses to execute the request
404 Not Found The server could not find the resource (web page) based on the client's request. With this code, web designers can set up a personalized page that says "the resource you requested could not be found"
405 Method Not Allowed method in client request is forbidden
406 Not Acceptable The server cannot fulfill the request based on the content characteristics of the client request
407 Proxy Authentication Required The request requires proxy authentication, similar to 401, but the requester should use the proxy for authorization
408 Request Time-out The server waits too long for the request sent by the client and times out
409 Conflict This code may be returned when the server completes the client's PUT request, and there was a conflict in the server's processing of the request
410 Gone The resource requested by the client no longer exists. 410 is different from 404. If the resource has been permanently deleted before, the 410 code can be used, and the website designer can specify the new location of the resource through the 301 code.
411 Length Required The server cannot process the request information sent by the client without Content-Length
412 Precondition Failed Client request information with wrong prerequisites
413 Request Entity Too Large The request was rejected because the requested entity was too large for the server to process. To prevent continuous requests from clients, the server may close the connection. If the server cannot process it temporarily, it will contain a Retry-After response message
414 Request-URI Too Large The requested URI (usually a URL) is too long for the server to process
415 Unsupported Media Type The server cannot handle the media format attached to the request
416 Requested range not satisfiable Invalid scope requested by client
417 Expectation Failed The server cannot satisfy the request header information of Expect
500 Internal Server Error Internal server error, unable to complete the request
501 Not Implemented The server does not support the requested function and cannot complete the request
502 Bad Gateway A server working as a gateway or proxy received an invalid response from the remote server when attempting to execute a request
503 Service Unavailable Due to overloading or system maintenance, the server is temporarily unable to process the client's request. The length of the delay can be included in the server's Retry-After header
504 Gateway Time-out The server acting as a gateway or proxy did not get the request from the remote server in time
505 HTTP Version not supported The server does not support the requested version of the HTTP protocol and cannot complete processing

Guess you like