HTTP request message and Response Message

Source: Internet
Author: User
Tags microsoft iis

HTTP packets are text-oriented. Each field in the packet is an ASCII string, and the length of each field is uncertain. HTTP has two types of packets: Request Message and Response Message.

 

1. Request Message
An HTTP Request Message consists of four parts: request line, request header, empty line, and request data.

(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 Request Parameters in the HTTP request data in the form of Names/values, and can transmit a large amount of data.

(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.

(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.

 

2. Details

An HTTP request consists of three parts: the request line, the message header, and the request body.

The request line starts with a method symbol and is separated by spaces, followed by the requested URI and Protocol version. The format is as follows:

Method Request-Uri http-version CRLF.

The method indicates the request method, the request-Uri is a unified resource identifier, the http-version indicates the HTTP protocol version of the request, and the CRLF indicates the carriage return and line feed (except for the CRLF as the end, separate CR or lf characters are not allowed ).

There are multiple request methods (all methods are capitalized). The methods are described as follows.

Get: request to obtain the resource identified by request-Uri.

Post: append new data to the resource identified by request-Uri.

Head: request to obtain the Response Message Header of the resource identified by request-Uri.

Put: The request server stores a resource and uses request-Uri as its identifier.

Delete: The request server deletes the resource identified by request-Uri.

Trace: the information of the request received by the request server. It is mainly used for testing or diagnosis.

Connect: reserved for future use.

Options: query the server performance or query resource-related options and requirements.

The method name is case sensitive. When the resource for a request does not support the corresponding request method, the server should return the status code 405 (method notallowed ); when the server does not recognize or does not support the corresponding request method, the status code 501 (notimplemented) should be returned ). The HTTP server should at least implement the get and head methods. Other methods are optional. Of course, the implementation supported by all methods should comply with the respective semantic definitions of the following methods. In addition to the preceding methods, the specific HTTP server can also extend the custom methods.

The following is an example of an application.

(1) Get method: when you enter a URL in the address bar of the browser to access the webpage, the browser uses the get method to obtain resources from the server.

For example: Get/form.html HTTP/1.1 (CRLF)

(2) Post method: the request server must accept the data attached to the request, which is often used to submit forms.

For example: Post/Reg. jsp HTTP/(CRLF)

  1. Accept: image/GIF, image/X-xbit,... (CRLF)
  2. ..
  3. HOST: www.nit.edu.cn (CRLF)
  4. Content-Length: 22 (CRLF)
  5. Connection: keep-alive (CRLF)
  6. Cache-control: No-Cache (CRLF)
  7. (CRLF) // The CRLF indicates the message
    The header has ended, before which it is the message header
  8. User = Jeffrey & Pwd = 1234 // the data submitted below this row

(3) Head Method: It is almost the same as get method. For the response part of the head request, its HTTP header contains the same information as the GET request. With this method, you do not need to transmit the entire resource content to obtain the information of the resource identified by request-Uri. This method is often used to test the validity, accessibility, and recent updates of hyperlinks.

A URL is "http://test.com/ask.asp? The following is an example of a GET request message with name = Liyang:

  1. Get http://test.com/ask.asp? Name = Liyang HTTP/1.1
  2. Accept :*/*
  3. Accept-language: ZH-CN
  4. Accept-encoding: gzip, deflate
  5. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;
  6. Windows NT 5.1; sv1;. Net CLR 2.0.50727)
  7. HOST: www.test.com
  8. Connection: keep-alive

2. Http response packets

HTTP response is composed of three parts: Status line, message header, and response body.

The status line format is as follows:

HTTP-version status-code reason-phrase CRLF

HTTP-version indicates the HTTP protocol version of the server, status-code indicates the response status code sent back by the server, and reason-phrase indicates the text description of the status code. The status code consists of three digits. the first digit defines the response category and has five possible values.

1xx: indicates that the request has been received and continues to be processed.

2XX: Success-indicates that the request has been successfully received, understood, and accepted.

3xx: Redirection-further operations are required to complete the request.

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

5xx: Server Error -- the server fails to fulfill the valid request.

Descriptions of common status codes and statuses are as follows.

200 OK: the client request is successful.

400 bad request: the client request has a syntax error and cannot be understood by the server.

401 unauthorized: the request is unauthorized. This status code must be used with the WWW-Authenticate header domain.

403 Forbidden: the server receives the request but rejects the service.

404 Not found: the requested resource does not exist. For example, an incorrect URL is entered.

500 internal server error: An Unexpected error occurs on the server.

503 server unavailable: the server cannot process client requests at present and may return to normal after a period of time. For example, HTTP/1.1200 OK (CRLF ).

A request URL is "http://test.com/ask.asp? The following is an example of a response message with name = Liyang:

    1. HTTP/1.1 200 OK
    2. Connection: keep-alive
    3. Date: Thu, 26 Jul 2010 14:00:02 GMT
    4. Server: Microsoft-Microsoft IIS/6.0
    5. X-powered-by: ASP. NET
    6. Content-Length: 280
    7. Content-Type: text/html
    8. Set-COOKIE: aspsessionidsaattcsq = joppkdcamh?oicjpgpbjob; Path =/
    9. Cache-control: Private
    10. <HTML>
    11. <Head>
    12. <Title> yiwangshen </title>
    13. </Head>
    14. <Body>
    15. <B> HTTP Response Message <br> </B>
    16. <B> test <br> </B>
    17. </Body>
    18. </Html>

HTTP request message and Response Message

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.