In layman's way: HTTP request mode

Source: Internet
Author: User
Tags ftp protocol

HTTP protocol (Hypertext Transfer Protocol, Hypertext Transfer Protocol), the HTTP protocol is located in the application layer, consisting of requests and responses. is the transport protocol used to transfer hypertext to the local browser from the WWW server . The hypertext information is stored on the Web server on the Internet, and the client needs to transmit the hypertext information it wants to access over the HTTP protocol.

The website address that we enter in the address bar of the browser is called the URL (Uniform Resource Locator, Uniform Resource Locator). Just like every household has a house address, each page has an Internet address. When you enter a URL in the Address box of the browser or click a hyperlink, the URL determines the address to be browsed. The browser uses Hypertext Transfer Protocol (HTTP) to extract Web page code from Web servers and translate them into beautiful web pages. HTTP request model: Always the client initiates the request and the server echoes the response. The main features of the HTTP protocol can be summarized as follows:

Supports client/server mode (CS).

(1) Simple and fast : When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has , POST. Each method specifies the type of client contact with the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.

(2) Flexible : HTTP allows the transfer of any type of data object. The type being transmitted is marked by content-type .

(3) no connection : The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.

(4) stateless : The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.

HTTP request mode

(1) Get: Get Resources

Get is generally used to get/query resource information, enter the url+ request parameter directly in the browser and click Enter to connect to the successful server to get the content.

The Get method is used to request resources that have been identified by the URI. The specified resource returns the response content after the server-side resolution (that is, if the requested resource is text, it is returned as it is, and if it is a CGI [ Universal Gateway Interface ] program, it returns the output after execution).
The Get method is most commonly used to query the server for certain information. If necessary, you can append the query string parameter to the end of the URL to send the information to the server. An error that often occurs when you use a GET request is that there is a problem with the format of the query string. The name and value of each parameter in the query string must use encodeurlcomponent () (see: a picture of encodeURI, encodeURIComponent, decodeURI, decodeURIComponent ) is encoded before it can be placed at the end of the URL, and all name-value pairs must be separated by (&), as in the following example:

Xhr.open ("Get", "01.php?name=foodoir&age=21", true);

The following function can be used to add query string parameters to the end of an existing URL:

1 function Addurlparam (url,name,value) {2URL + = (Url.indexof ("?") == -1?"?":"&");3URL + = encodeuricomponent (name) +"="+encodeURIComponent (value);4         returnURL;5}

This Addurlparam function accepts three parameters: the URL to add the parameter to, the name of the parameter, and the value of the parameter. The following is an example of using this function to construct a URL:

1     varURL ="example.php";2     //Adding Parameters3url = addurlparam (URL,"name","Foodoir");4url = addurlparam (URL," Age"," +");5     //Initializing requests6Xhr.open ("Get"Urlfalse);

The Get method is the default HTTP request method, and we routinely use the Get method to submit form data, but the form data submitted with the Get method is simply encoded, and it is sent to the Web server as part of the URL, so If you use the Get method to submit form data, there is a security risk. For example

Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB from the URL request above, it is easy to identify what the form submits. (? ) In addition, the amount of data submitted cannot be too large because the data submitted by the Get method is part of the URL request.

(2) POST: Transfer entity text

The Post method is used to transfer the body of the entity. Although the body of the entity can also be transferred with the Get method, it is generally not transmitted using the Get method, but the Post method is used, although the Get method is similar to the Post method, but the main purpose of post is not to get the main content of the response. The body of a POST request can contain very much data and is not limited in format.

Here we compare the difference between the Get method and the Post method in nature:

1) The Get method is used for information acquisition, it is secure (security: Refers to non-modified information, such as database information), and the Post method is used to modify the server resources on the request;

2) The data of the GET request is appended to the URL, and the data submitted by the Post method is placed in the body of the HTTP message entity, so the Post method is more secure than the Get method.

3) The amount of data transferred by the Get method is generally limited to 2KB, because the get is to submit data through a URL, and the URL itself has no restrictions on the data, but different browsers have restrictions on the URL, such as Internet Explorer for URL restrictions of 2KB, and Chrome, The Firefox browser theoretically has no limit on the URL, its real limit depends on the operating system itself; The Post method is unlimited for data size, and the ability to really affect the size of the data is the server handler.

4) The Get method is the default HTTP request method, we use the Get method to submit the form data, but the form data submitted with the Get method is simply encoded, and it is sent as part of the URL to the Web server, so If you use the Get method to submit form data, there is a security risk. For example

Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB from the URL request above, it is easy to identify what the form submits. (? ) In addition, the amount of data submitted cannot be too large because the data submitted by the Get method is part of the URL request.

5) The Post method is an alternative to the Get method, which is primarily to submit form data to the Web server, especially large batches of data. The Post method overcomes some of the drawbacks of the Get method. When submitting form data through the Post method, the data is not sent as part of the URL request but as standard data to the Web server, which overcomes the drawback that the information in the Get method is not confidential and the amount of data is too small. Therefore, for security reasons and respect for user privacy, the Post method is usually used for form submission.

(3) HEAD: Get message header
The head method, like the Get method, simply does not return the main part of the text to confirm the validity of the URI and the date and time of the resource update.
Specifically: 1, the judgment type; 2. Check the status code in the response to see if the object exists (response: The request executed successfully, but no data returned); 3. Test resources have been modified
The difference between the head method and the Get method: The Get method has entities, and the Head method has no entities.

(4) PUT: Transfer file

The Put method is used to transfer files, just like FTP protocol file uploads, requiring that the contents of the file be included in the body of the request message, and then saved in the location specified by the request URI. However, http/1.1 put method itself without authentication mechanism, anyone can upload files, there are security issues, it is generally not used.

(5)Delete: Delete file

Indicates that the client wants the server to delete a resource, as opposed to the Put method, which deletes the specified resource by URI

(6)options: How to ask for support

The options method is used to query the method that specifies the resource support for the request URI (the client asks which request methods the server can submit)

(7)trace: Trace Path

The client can trace the transmission path of the request message by allowing the Web server to communicate the previous request back to the client's method

(8) Connect: Requires a tunneling protocol to connect the agent

The Connect method requires a tunnel to be established when communicating with a proxy server to enable TCP communication with the tunneling protocol. The main use of SSL (Secure Sockets Layer) and TLS (Transport Layer Security) protocol to encrypt the communication after the network tunnel transmission.

The above section reference:HTTP request method detailed

HTTP request Process

A complete HTTP request process begins when the connection to the TCP three handshake is successful, and the client begins sending an HTTP request to the server in the specified format, after the server receives the request, resolves the HTTP request, processes the business logic, and returns an HTTP response to the client. The HTTP response content also has a standard format. No matter what the client or the server, as long as the HTTP protocol standards to achieve, then it must be universal.

HTTP Request Format

The HTTP request format consists of four main parts: the request line, the request header, the blank line, the message body , each part contains one row

1 <request-line>2 <general-headers>3 <request-headers>4 < Entity-headers>5 <empty-line>6 [<message-body>]

Request Line : The request line is the first line of the request message and consists of three parts: the request method (Get/post/delete/put/head), the URI path of the request resource, the version number of the HTTP

1 get/index.html http/1.1

Request Header : The information in the request header has a cache-related header (cache-control,if-modified-since), Client identity information (user-agent), and so on. For example:

 1  cache-control:max-age=0  2  cookie:gsscrollpos=; _ga=ga1. 2.329038035 . 1465891024 ; _gat=1  3  If-modified-since:sun, 01  may 2016  11 : 19 : 03   GMT  4  user-agent:mozilla/5.0  (Windows NT 10.0 ; WOW64) Applewebkit/537.36  (khtml, like Gecko) chrome/ 51.0 . 2704.84  safari/537.36  

message body : The request body is the client's request data to the server, this part of the data is not required for each request.

HTTP Response Format

The server returns an HTTP corresponding message to the client after the request has been processed. The format of the HTTP response message includes: status line, response header, blank line, message body . Each part contains one row.

1 <status-line>2 <general-headers>3 <response-headers>4 < Entity-headers>5 <empty-line>6 [<message-body>]

status line : The status line is located in the first line of the corresponding message, with the HTTP protocol version number, status code and status description three parts. Such as:

1 http/1.1 OK

Response Headers : The response header is some information that the server passes to the client to describe the server and the policy when it continues to access the resource in the future.

1 connection:keep-alive2 content-encoding:gzip3 content-type:text/ html charset=utf-84:  GMT  5 server:nginx/1.9.  A 6 transfer-encoding:chunked

Response Body: The response body is the HTML text content returned to the client by the server, or data in other formats, such as video streams, pictures, or audio data.

Part of the above reference: a complete HTTP request process

In layman's way: HTTP request mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.