HTTP requests and status codes

HTTP request and response

HTTP communication consists of two parts: client request message and server response message

 

(1) The process of the browser sending the HTTP request:

When the user enters a URL in the browser's address bar and presses the Enter key, the browser sends an HTTP request to the HTTP server. HTTP requests are mainly divided into "Get" and "Post" methods.

When we enter the URL http://www.baidu.com in the browser, the browser sends a Request request to get the html file of http://www.baidu.com, and the server sends the Response file object back to the browser .

The browser analyzes the HTML in the Response and finds that many other files are referenced, such as Images files, CSS files, and JS files. The browser will automatically send the Request again to get the image, CSS file, or JS file.

When all the files are downloaded successfully, the web page will be completely displayed according to the HTML syntax structure.

 

[URL (abbreviation for Uniform / Universal Resource Locator)]:

Uniform Resource Locator is an identification method used to completely describe the addresses of web pages and other resources on the Internet.

[Basic format:] scheme://host[:port#]/path/…/[?query-string][#anchor]

                        protocol: //IP[:port#]/path/.../[?parameter][#anchor]

【Analysis:】

(1) scheme: protocol (for example: http, https, ftp)

(2) host: IP address or domain name of the server

(3) port#: the port of the server (if it is the default port of the protocol, the default port is 80)

(4) path: the path to access the resource

(5) query-string: parameter, the data sent to the http server

(6) anchor: anchor (jump to the specified anchor position of the web page)

E.g:

http://www.baidu.com
http://192.168.0.116:8080/index.jsp
http://item.jd.com/11936238.html#product-detail

 

(2) Client HTTP request:

The URL just identifies the location of the resource, while HTTP is used to submit and fetch the resource.

The client sends an HTTP request to the server's request message, which includes the following formats: request line, request header, blank line, and request data.

HTTP requests are mainly divided into two methods: Get and Post:

  GET is to get data from the server, POST is to send data to the server

  The parameters of the GET request are displayed on the browser website. The HTTP server generates the response content according to the parameters in the URL contained in the request, that is, the parameters of the "Get" request are part of the URL. For example: http://www.baidu.com/s?wd=Chinese

  POST request parameters are in the request body, the message length is unlimited and sent in an implicit way. It is usually used to submit a relatively large amount of data to the HTTP server (for example, the request contains many parameters or file upload operations, etc.), the request parameters Included in the "Content-Type" header, indicating the media type and encoding of the message body,

  Note: Avoid using the Get method to submit forms, as it may lead to security issues. For example, using the Get method in the login form, the user name and password entered by the user will be exposed in the address bar.

Commonly used request headers: I will introduce this in a later article

 

 

When a browser makes a request to a web server, it passes a block of data to the server, which is the request information

The HTTP request information consists of 3 parts:

①Request method URI protocol/version; ②Request Header; ③Request body 

The following is an example of an HTTP request:

GET/sample.jspHTTP/1.1
Accept:image/gif.image/jpeg,*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
Accept-Encoding:gzip,deflate
username=jinqiao&password=1234

 (1) Request method URI protocol/version 

The first line of the request is "method URL proposal/version": GET/sample.jsp HTTP/1.1 

In the above code, "GET" represents the request method, "/sample.jsp" represents the URI, and "HTTP/1.1 represents the protocol and the version of the protocol. 

According to the HTTP standard, HTTP requests can use various request methods. For example: HTTP1.1 supports 7 request methods: GET, POST, HEAD, OPTIONS, PUT, DELETE and TARCE. In Internet applications, the most commonly used methods are GET and POST. 

The URL completely specifies the network resource to be accessed, usually only a relative directory relative to the server's root directory is given, so it always starts with "/", and finally, the protocol version declares the version of HTTP used in the communication process.

(2) Request Header 

The request headers contain a lot of useful information about the client environment and the request body. For example, request headers can declare the language used by the browser, the length of the request body

Accept:image/gif.image/jpeg.*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible:MSIE5.01:Windows NT5.0)
Accept-Encoding:gzip,deflate.

 (3) Request body 

There is a blank line between the request header and the request body, this line is very important, it means that the request header has ended, and the next is the request body. The request body can contain query string information submitted by the client: 

username=jinqiao&password=1234

 In the HTTP request of the above example, the body of the request has only one line of content. Of course, in practical applications, the HTTP request body can contain more content. 

 

The HTTP response is similar to the HTTP request. The HTTP response also consists of 3 parts, namely: 

①Protocol status version code description; ②Response Header; ③Response body 

Here is an example of an HTTP response:

HTTP/1.1 200 OK
Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:23:42 GMT
Content-Length:112
<html>
<head>
<title>Example HTTP response <title>
</head>
<body>
Hello HTTP!
</body>
</html>

 The protocol status code describes the first line of the HTTP response is similar to the first line of the HTTP request, it indicates that the protocol used for communication is HTTP1.1 The server has successfully processed the request sent by the client (200 indicates success): 

HTTP/1.1 200 OK 

The response header (Response Header) The response header also contains a lot of useful information like the request header, such as server type, date time, content type and length, etc.:

Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:13:33 GMT
Content-Type:text/html
Last-Moified:Mon,6 Oct 2003 13:23:42 GMT
Content-Length:112

 Response body The response body is the HTML page returned by the server:

<html>
<head>
<title>Example HTTP response <title>
</head>
<body>
Hello HTTP!
</body>
</html>

 The response header and body must also be separated by a blank line.

HTTP protocol versions supported by common methods

Methods supported by HTTP1.0 and HTTP1.1

 

 

 

 

 

【HTTP status code】

HTTP Status Code is a 3-digit code used to indicate the status of the HTTP response of the web server

An error may occur when the browser requests a service from a web server

As a result, the following series of status messages may be returned:

(1) 1xx: Information

(2) 2xx: success

(3) 3xx: Redirect

(4) 4xx: Client error (also called request error)

(5) 5xx: server error

 

1xx:

On behalf of the request has been accepted and needs to continue processing. This type of response is a provisional response, containing only the status line and some optional response header information, and terminated by a blank line. Since no 1xx status codes are defined in the HTTP/1.0 protocol, the server MUST NOT send 1xx responses to such clients except under certain experimental conditions.

 

2xx:

Indicates that the request has been successfully received, understood, and accepted by the server

 

3xx:

Indicates that further action from the client is required to complete the request. Usually, these status codes are used for redirection, and the subsequent request address (redirect target) is indicated in the Location field of this response.

If and only if the method used by the subsequent request is GET or HEAD, the user's browser can automatically submit the required subsequent request without user intervention. Clients should automatically detect infinite loop redirects (eg: A->A, or A->B->C->A), as this can lead to a lot of unnecessary resource consumption on both server and client. As recommended by the HTTP/1.0 specification, browsers should not automatically visit more than 5 redirects.

 

4xx:

Indicates that an error may have occurred on the client side, preventing the server from processing. Unless responding to a HEAD request, the server SHOULD return an entity explaining the current error condition and whether this is a temporary or permanent condition. These status codes apply to any request method. Browsers SHOULD display to the user any physical content included in such error responses.

If the client is transmitting data when the error occurs, server implementations using TCP should take care to ensure that the client has received the packet containing the error message before closing the connection between the client and the server. If the client continues to send data to the server after receiving the error message, the server's TCP stack will send a reset packet to the client to clear all unrecognized input buffers for the client, so that the data is not blocked by the server on the server. Applications read and interfere with the latter.

 

5xx:

It means that the server has an error or abnormal state in the process of processing the request, or it may be that the server realizes that the current software and hardware resources cannot complete the processing of the request. Unless this is a HEAD request, the server SHOULD contain an informational entity explaining the current error status and whether the status is temporary or permanent. The browser SHOULD present to the user any entities included in the current response.

These status codes apply to any response method.

 

【Simple Version】

100 Continue Continue, generally when sending a post request, the server will return this information after the http header has been sent, indicating confirmation, and then send specific parameter information

 

200 OK Normal return information

201 Created The request was successful and the server created a new resource

202 Accepted The server has accepted the request but has not yet processed it

 

301 Moved Permanently The requested page has been permanently moved to a new location.

302 Found Temporary redirect.

303 See Other Temporary redirect and always use GET to request a new URI.

304 Not Modified The requested page has not been modified since the last request.

 

400 Bad Request The server does not understand the format of the request and the client SHOULD NOT attempt to make a request with the same content again.

401 Unauthorized The request is not authorized.

403 Forbidden Forbidden.

404 Not Found The resource was not found how to match the URI.

 

500 Internal Server Error The most common server-side error.

503 Service Unavailable The server is temporarily unable to process the request (perhaps due to overload or maintenance).

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326263691&siteId=291194637