7 steps to a complete HTTP request

Source: Internet
Author: User

The HTTP communication mechanism is that during a complete HTTP communication, the following 7 steps will be completed between the Web browser and the Web Server:

1. Establishing a TCP connection
Before HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done through tcp, which works with the IP protocol to build the internet, known as the TCP/IP protocol family, so the Internet is also known as a TCP/IP Network. HTTP is a higher level of application-level protocol than tcp, according to the rules, only the lower layer protocol is established before a higher-level protocol connection, therefore, the first to establish a TCP connection, the port number of the general TCP connection is 80.

2. Web browser sends request command to Web server
Once a TCP connection is established, the Web browser sends a request command to the Web Server. For Example: get/sample/hello.jsp http/1.1.

3. Web browser sends request header information
After the browser sends its request command, it also sends some other information to the Web server in the form of header information, and then the browser sends a blank line to notify the server that it has ended sending the header Information.

4. Web Server Answer
After the client makes a request to the server, the server responds back to the client, http/1.1, and the first part of the answer is the version number of the protocol and the response status Code.

5. The Web server sends the answer header information
Just as the client sends information about itself along with the request, the server also sends the user with the answer about its own data and the requested Document.

6. The Web server sends data to the browser
After the Web server sends the header information to the browser, it sends a blank line to indicate that the header information is sent to the end, and then it sends the actual data requested by the user in the format described in the Content-type reply header Information.

7. The Web server shuts down the TCP connection
In general, once the Web server sends the request data to the browser, it closes the TCP connection and then if the browser or server joins this line of code in its header: connection:keep-alive

The TCP connection remains open after it is sent, 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.

TCP through three handshake to establish a reliable connection, this article does not do much to repeat, here is the main point of the client when initiating an HTTP request, what data is sent in the packet, and the request to successfully receive the Web server packet;

Here you grab the packets that the mobile app sends to the Web server via the fiddler tool:


HTTP request is Http://xg.mediportal.com.cn/health/sms/verify/telephone

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

HTTP request information by 3 part of the composition:

1. Request Method (get/post),URI, protocol /version

2. Request Header

3. Request BODY

To do an analysis of:

POST Http://xg.mediportal.com.cn/health/sms/verify/telephone http/1.1

user-agent:dgrouppatient/1.052701.230/dalvik/2.1.0 (Linux; U Android 5.1.1; Kiw-al10 build/honorkiw-al10)
content-type:application/x-www-form-urlencoded; Charset=utf-8
Host:xg.mediportal.com.cn
Connection:keep-alive
Accept-encoding:gzip
Content-length:33

telephone=15527177736&usertype=1&

(1) Request method,URI, protocol /version

The first line of the request is "method,URL, protocol /version":

POST Http://xg.mediportal.com.cn/health/sms/verify/telephone http/1.1

In the code above, "POST" represents the request method, "http://xg.mediportal.com.cn/health/sms/verify/telephone" represents the URI, "http/ 1.1 represents the version of the agreement and Protocol.

HTTP requests can use a variety of request methods, depending on the HTTP Standard. For Example:HTTP1.1 currently supports 7 methods of Request:GET,POST,HEAD,OPTIONS,PUT, delete,and tarce.

GET

Request to get the resource identified by Request-uri

POST

Append new data to the resource identified by Request-uri

HEAD

Request for a response message header for a resource identified by Request-uri

OPTIONS

Request performance of the query server, or query for resource-related options and requirements

PUT

The request server stores a resource and uses Request-uri as its identity

DELETE

Requesting the server to delete resources identified by Request-uri

TRACE

Request Server Loopback received Request information, Primary language test or diagnostics

In Internet applications, The most common method is get and POST. finally, the protocol version declares the version of HTTP that is used during the communication process.

(2) Requesting header ( request Header)

The request header contains many useful information about the client environment and the request Body. For example, the request header can declare the language used by the browser, the length of the request body, and so On.

user-agent:dgrouppatient/1.052701.230/dalvik/2.1.0 (Linux; U Android 5.1.1; KIW-AL10 build/honorkiw-al10)//client environment where the user sends the request
content-type:application/x-www-form-urlencoded; charset=utf-8// Format of default submission data for forms
Host:xg.mediportal.com.cn// intenet Host and port number of the requested resource
Connection:keep-alive//persistent Connection
Accept-encoding:gzip//browser can decode the data encoding method
Content-length:33//length of request body

Content-type

is a very important part of the return message, indicating what MIME type the following document belongs To. Content-type: [type]/[subtype]; Parameter For example, the most common is text/html, which means that the returned content is a text type, and this text is in HTML format. In principle, the browser will decide how to display the returned message body content according to Content-type.

Host

Specifies the intenet host and port number of the requesting resource, which must represent the location of the originating server or gateway that requested the URL. The http/1.1 request must contain the host header domain or the system will return with a 400 status code

Accept

Browser acceptable MIME types

Accept-charset

browser-acceptable Character Set

Accept-encoding

The data encoding that the browser can decode, such as Gzip. The servlet can return a gzip-encoded HTML page to a browser that supports GZIP. In many cases this can reduce download time by 5 to 10 times times

Accept-language

The type of language the browser wishes to use when the server is able to provide more than one language version

Authorization

Authorization information, usually in response to a www-authenticate header sent to the server

Connection

Indicates whether a persistent connection is Required. If the servlet sees the value here as "keep-alive", or sees the request using HTTP1.1 (HTTP 1.1 is persistent by default), it can take advantage of the persistent connection, when the page contains multiple elements (such as applets, pictures), Significantly reduce the time it takes to Download. To do this, the servlet needs to send a content-length header in the answer, and the simplest implementation is to write the content to Bytearrayoutputstream first and then calculate its size before formally writing the content

Content-length

Indicates the length of the request message body

Cookies

This is one of the most important request header information

From

The email address of the requesting sender, which is used by some special web client, is not used by the browser

Host

Hosts and ports in the initial URL

If-modified-since

Returns 304 "not Modified" answer only if the requested content has been modified after the specified date to return it

Pragma

Specifying a "no-cache" value means that the server must return a refreshed document, even if it is a proxy server and has a local copy of the page

Referer

Contains a URL where the user accesses the currently requested page from the page represented by the URL

User-agent

Browser type, This value is useful if the content returned by the servlet is related to the browser type

Ua-pixels,ua-color,ua-os,ua-cpu

Non-standard request headers sent by some versions of Internet Explorer to indicate screen size, color depth, operating system, and CPU type

The common MIME types are as Follows:

    •     text/html:html format
    •     text/plain: Plain Text Format     & nbsp; 
    •     text/xml:  XML format
    •     image/gif:gif picture format  & nbsp;  
    •     image/jpeg:jpg picture format  
    •     image/ Png:png picture Format

      Media format type starting with application:

    • application/xhtml+xml:xhtml format
    • Application/xml:xml data format
    • Application/atom+xml:atom XML Aggregation format
    • Application/json:json data format
    • Application/pdf:pdf format
    • Application/msword:word Document Format
    • Application/octet-stream: binary stream Data (e.g., common file Downloads)
    • Application/x-www-form-urlencoded: <form enctype= "" > Default enctype,form form data is encoded as key/ The value format is sent to the server (the format of the Form's default submission Data)

Another common media format is used when uploading files:

    • Multipart/form-data: when you need to upload a file in a form, you need to use that format

(3) Request Body

Between the request header and the request body is a blank line, which is very important, which indicates that the request header has ended, followed by the request Body. The request body can contain query string information submitted by the Customer:

telephone=15527177736&usertype=1&

HTTP response Format

HTTP replies are similar to HTTP requests, andHTTP responses are made up of 3 parts, namely:

1, status Line

2. Response header (Response header)

3. Response body

http/1.1 Ok//status Line
Server:nginx
date:tue, 02:09:24 GMT
Content-type:application/json;charset=utf-8
Connection:keep-alive
Vary:accept-encoding
Access-control-allow-origin: *
access-control-allow-headers:x-requested-with,access_token,access-token,content-type,multipart/form-data, application/x-www-form-urlencoded
Access-control-allow-methods:get,post,options
content-length:49

{"resultcode": 1, "resultmsg": "phone number not registered"}//body

(1) status Line

By the protocol version, the status code in the form of a number, and the corresponding status description, each element is separated by a space.

Status Code:

The status code consists of 3 digits that indicate whether the request is understood or is Satisfied.

Status description:

The status description gives a short textual description of the status Code.

The first number of the status code defines the category of the response, and the following two bits do not have a specific classification.

The first number has five possible values:

-1xx: indicates information-indicates that the request has been received and continues Processing.

-2xx: Success-indicates that the request has been successfully received, understood, Accepted.

-3xx: Redirect-a Further action must be made to complete the Request.

-4xx: client error-the request has a syntax error or the request cannot be Implemented.

-5xx: server-side error-the Server failed to implement a legitimate request.

Status Code Status Description description

OK Client Request succeeded

The bad request is not understood by the server because of a syntax error in client Requests.

401 unauthonzed request is not Authorized. This status code must be used with the Www-authenticate header field

The 403 Forbidden server received the request but refused to provide the Service. The server typically gives reasons for not serving in the response body

404 Not Found The requested resource does not exist, for example, the wrong URL was Entered.

The Internal server error server has unexpected errors that could result in the Client's request not being Completed.

The 503 Service unavailable server is currently not able to process client requests, and after a period of time the server may return to normal

(2) Response Head

The response header may include:

location :  

The Location response header field is used to redirect the recipient to a new Position. For example: the client requested the page no longer exists in the original location, in order to redirect the client to the new location of this page, the server can send back to the address of the response header after the use of redirection statements, let the client access to the new domain name corresponding to the resources on the Server. When we use the redirect statement in the jsp, the server side sends back the response header to the client, and there is a location response header Field.

Server:

The server Response header field contains the software information that the server uses to process the Request. It corresponds to the user-agent request header domain, which sends information about the Server-side software, which sends the client software (browser ) and the operating System. The following is an example of the Server response header field:server:apache-coyote/1.1

Www-authenticate:

The Www-authenticate response header field must be contained in a 401 (unauthorized ) response message, and the header domain is related to the authorization Request header field mentioned earlier when the client receives a 401 response message, Decide whether to request the server to validate it. If the server is required to validate it, a request containing the authorization header domain can be sent, and here is An example of the Www-authenticate response header field:www-authenticate: Basic realm= "basic Auth test!"

From this response header domain, you can know that the server side is using the Basic authentication mechanism for the resources we Request.

content-encoding :

The Content-encoding Entity header field is used as the modifier for the media type, and its value indicates the additional content encoding that has been applied to the entity body, so the corresponding decoding mechanism must be used to obtain the media type referenced in the Content-type header Domain. content-encoding The main terms of the document compression method, here is an example: content-encoding:gzip. If an entity body is stored in an encoded manner, it must be decoded before it is Used.

Content-language:

The Content-language Entity header field describes the natural language used by the Resource. Content-language allows users to identify and differentiate entities according to their preferred language. If the entity content is intended only for Danish readers, the entity header field can be set as Follows:Content-language:da.

If the content-language header field is not specified, then the entity content is provided to the reader of the Language.

Content-length :

The Content-length Entity header field is used to indicate the length of the body, expressed as a decimal number stored in bytes, that is, a numeric character occupies one byte and is transmitted using its corresponding ASCII code STORAGE.

Note that this length is only the length of the entity body and does not include the length of the entity Header.

Content-type:

The Content-type Entity header field term indicates the media type that is sent to the Recipient's entity Body. For example:

Content-type:text/html;charset=iso-8859-1

content-type:text/html;charset=gb2312

Last-modified:

The Last-modified Entity header field is used to indicate the last modification date and time of the Resource.

Expires:

The Expires Entity header field gives the date and time when the response Expires. typically, a proxy server or browser caches some Pages. When the user accesses these pages again, it is loaded directly from the cache and displayed to the user, which shortens the response time and reduces the load on the Server. In order for the proxy server or browser to update the page after a period of time, we can use the Expires Entity header field to specify when the page Expires. When the user accesses the page again, if the date and time given by the Expires header field are earlier (or the Same) than the date and time given by the date normal header field , then the proxy server or browser will no longer use the cached page but instead request the updated page from the Server. note, however, that even if the page expires, it does not mean that the original resource on the server has changed before or after this Time.

The date and time used by the expires Entity header field must be a date format in RFC 1123, for example:

expires:thu, SEP 2005 16:00:00 GMT

The HTTP1.1 client and cache must treat other illegal date formats (also including 0) as Expired. For example, to let the browser do not cache the page, we can also take advantage of the Expires entity header field, set its value to 0, as follows (JSP):Response.setdateheader ("Expires", 0);

7 steps to a complete HTTP request

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.