Let's talk about the theory:
(1) Request Line
A request line consists of three fields: Request Method Field, URL field, and HTTP Protocol version field, which are separated by spaces. For example, get/index.html HTTP/1.1.
HTTP request methods include get, post, head, put, delete, options, trace, and connect. The most common get and post methods are described here.
Get: when the client wants to read documents from the server, use the get method. The get method requires the server to place the URL-located resource in the data part of the Response Message and send it back to the client. When the get method is used, the request parameters and corresponding values are appended to the URL, and a question mark ("?") is used. It indicates the end of the URL and the start of the request parameter. The length of the transfer parameter is limited. For example,/index. jsp? Id = 100 & OP = bind.
Post: the post method can be used when the client provides more information to the server. The post method encapsulates the Request Parameters in the HTTP request data in the form of Names/values. A large amount of data can be transmitted and can be used to transmit files.
(2) Request Header
The request header consists of a pair of keywords and values. Each line is a pair. The keywords and values are separated by a colon. The request header notifies the server of client request information. Typical request headers include:
User-Agent: browser type of the request.
Accept: List of content types that can be recognized by the client.
HOST: the requested host name, which allows multiple domain names to share one IP address, that is, the virtual host.
(3) Empty rows
The last request header is followed by an empty line that sends a carriage return and line feed, notifying the server that there are no request headers below.
For a complete HTTP request, empty rows are required. Otherwise, the server considers that the data in this request has not been completely sent to the server and is in the waiting state.
(4) request data
The request data is not used in the GET method, but in the POST method. The POST method is applicable when the customer needs to fill out the form. The most frequently used request headers related to request data are Content-Type and Content-Length.
(5) Request example
POST:
The POST packet header is as follows:
POST/sn/index. php HTTP/1.1
Accept :*/*
Accept-Language: zh-cn
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 12
Connection: close
Sn = 123 & n = asa
There is a blank line behind the http header, and the blank line is followed by sending post data. The Length is indicated by Content-Length: 12
Indicates that the post data contains two items.
Sn = 123
N = asa
Content-Type: application/x-www-form-urlencoded specifies the encoding Type of POST data.
Content-Length: 12 POST Data Length
GET:
The GET request header is as follows:
GET/sn/index. php? Sn = 123 & n = asa HTTP/1.1
Accept :*/*
Accept-Language: zh-cn
Host: localhost
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: 12
Connection: Close
Let's look at the difference:
1. When a GET request is used, the parameter is displayed in the URL, but the POST method is not displayed.
2. A small amount of data is sent using the GET request, and a large amount of data is sent using the POST request.
3. cache problem: GET requests need to pay attention to the cache problem. POST requests do not need to worry about this problem.
4. For post requests, the Content-Type value must be set to application/X-form-WWW-urlencoded,
When a request is sent, because the GET request parameters are in the URL, the XMLHTTP object send function sends a null parameter, while the POST request uses the XMLHTTP object send method, but you need to assign its parameters
In either way, the data obtained from the server is consistent.
The GET request URL contains parameters. The POST request URL does not contain parameters. The POST request will not be cached.
When to use GET requests and post requests
The purpose of a GET request is to give the server some parameters to obtain the list from the server. For example: List. aspx? Page = 1, indicating to get the data on the first page
The purpose of the POST request is to send some parameters to the server, such as the content in form.