HTTP request Process

Source: Internet
Author: User
Tags http post

Reference: HTTP POST GET essential differences in detail

Suppose the request url:http://www.helloworld.com:8080/page/index.html

1. Establish a connection

First, DNS resolves the www.helloworld.com and maps it to the appropriate IP address.

If there is a port number in the URL, the port number is used. Otherwise, the port number uses the protocol's default port number. For example, the default port number for the HTTP protocol is 80.

With the IP address and port number, the TCP connection is started through three handshakes.

2. The client sends the request

Once the connection is successfully established, the client can begin sending requests to the server. This request type is usually get or post, and there are some infrequently used put, delete, and so on.

Format of Request:

<request line>

<blank line>

<request body>

Description

1) Request line: In an HTTP request, the first line must be a request row that describes the type of request, the resources to be accessed, and the HTTP version used.

2) Header: Used to describe additional information that the server will use.

3) blank line: Used to separate the header and request body.

4) Request body: The body, you can add any other data.

Get Request Example:

#get/books/?sex=man&name=professional http/1.1     # below are all headers. Describes the host to access, the browser used is "mozilla/5.0" and so on.  Host:www.wrox.comUser-agent:mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.7.6) Gecko/20050225 firefox/1.0.1connection:keep-alive

Example of a POST request:

# request line, the requested type is post, the resource to access is "/", the HTTP version used is 1.1 post/http/1.1#  header, describes the host to access, the browser used, the body part of the format and the length of the transfer.  Host:www.wrox.comUser-agent:mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.7.6) Gecko/20050225 firefox/1.0.1Content-type:application/x-www-form-  Urlencodedcontent-length:40connection:keep-Alive#blankLine#  Request BodyName=professional%20ajax&publisher=wiley

Note:

1) The params of the GET request.

The parameters of the GET request are displayed directly in the URL, and we can see the parameters in Request line.

URL adoption? To split the previous and subsequent transfer parameters. Multiple transfer parameters with & connection.

The parameters are in the Key-value pair mode.

For example: Login.action?name=hyddd&password=idontknow&verify=%e4%bd%a0%e5%a5%bd.

2) The encoding format of the URL is ASCII, not Unicode, which means that you cannot include any non-ASCII characters in the URL, and all non-ASCII characters need to be encoded and retransmitted.

3) The data of the GET request is usually placed inside the URL, and the transmit data of the post request is placed in the request body.

4) Content-type

This surface body returns the file format. For example, the post Content-type is "application/x-www-form-urlencoded", indicating that the data transmitted is in form format. The others are "text/xml", which means that the body part is in XML format.

3. Server response

After the server receives the request, it processes and returns the data for the response.

Format of HTTP response:

<status line>

<blank line>

<response body>

Description

1) status line: Used to indicate HTTP version, and answer status code.

Common Status Codes:

(OK): The resource was found and everything is OK.

304 (not MODIFIED): The resource has not been modified since the last request. Typically used for the browser's caching mechanism.

401 (Unauthorized): The client does not have permission to access the resource. This usually causes the browser to require the user to enter a user name and password to log on to the server.

403 (FORBIDDEN): Client failed to get authorization. This is usually followed by an incorrect user name and password after 401.

404 (Not FOUND): The requested resource does not exist at the specified location.

Example:

http/1.1 DEC 2005 23:59:59 gmtcontent-type:text/html;charset=iso-8859-1 Content- length:122    Wrox homepage  
   ></body>
4. Close the connection

In general, once the server sends the response data to the browser, it closes the TCP connection. The TCP connection is closed with four breakup times.

However, if the browser or server adds "connection:keep-alive" to its header, the TCP connection will remain open after sending, so the browser can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.

HTTP request Process

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.