[Excerpt] HTTP request model and header information

Source: Internet
Author: User
Tags md5 digest response code rfc

HTTP request Model

1. Connect to the Web server
A client application (such as a web browser) open a socket (80 by default) to the http port of the Web server ).

example: http://www.myweb.com: 8080/index.html
in Java, this is equivalent to Code :
soceet socket = new socket ("www.myweb.com", 8080);
inputstream in = socket. getinputstream ();
outputstream out = socket. getoutputstream ();

2. Send an HTTP request
the client writes an ASCII text request line through a connection, followed by 0 or multiple HTTP header labels, one blank line and any data requested.

A request consists of four parts: request line, request header mark, empty row, and request data

1. request Line: The request line consists of three tags: Request Method, request URI, and HTTP Version. They are separated by spaces.
example: Get/index.html HTTP/1.1

the HTTP standard defines eight possible request methods:
a simple request that identifies a resource in the get retrieval URI
the head and get methods are the same, the server returns only the status line and header mark, the request document is not returned.
the Post Server accepts the data written to the output stream of the client.
The put server saves the request data as the new content of the specified URI.
Delete the server. requests for resources named in Uri
options requests for server-supported Request Method Information
trace Web Server feedback HTTP requests and requests with their headers
connect documentation but is not implemented yet, reserved for tunneling

2. Request Header: it consists of key/value pairs. Each line has a pair. The keywords and values are separated by colons. The request header is used to notify the server of the functions and identifiers of the client. A typical request header is marked:

User-Agent client manufacturer and version
List of content types that can be recognized by the accept client
Number of bytes of Content-Length appended to the request

3. blank line: the last request header is followed by an empty line, which sends a carriage return and a fallback message, notifying the server that there is no header mark below.
4. Request data: the Content-Type and Content-Length headers are usually used for data transmission using post.

3. The server accepts the request and returns an HTTP Response

The Web server parses the request and locates the specified resource. The server writes a copy of the resource to the socket, which is read by the client.

A response consists of four parts: Status line, Response Header mark, blank line, and response data.

1. Status line: the Status line consists of three tags: HTTP Version, response code, and response description.
HTTP Version: specify the maximum version that the client can understand.
Response Code: A three-digit numeric code that indicates whether the request succeeds or fails. If the request fails, it indicates the reason.
Response Description: the readability of the response code.
Example: HTTP/1.1 200 OK

HTTP response code:
1xx: information, request received, continue processing
2XX: Successful, accepted, understood, and accepted
3xx: redirection. actions that must be performed further to complete the request
4xx: client error:
2. Response Header: like the request header, they indicate the functions of the server and identify the details of the response data.
3. blank line: the last response header is followed by an empty line. The carriage return and fallback are sent, indicating that no header mark is available below the server.
4. Response Data: HTML documents and images, that is, HTML itself.

4. The server closes the connection and the browser parses the response.
1. the browser first parses the status line to check the status code indicating whether the request is successful.
2. parse each response header, and the header Mark tells the following several bytes of HTML.
3. Read the response data HTML, format it according to the syntax and semantics of the HTML, and display it in the browser window.
4. An HTML document may contain references to other resources to be loaded. the browser recognizes these references and makes additional requests to other resources. This process is repeated multiple times.

5. Stateless connection
The HTTP model is stateless, indicating that the Web server does not remember requests from the same client when processing a request.

Vi. Instances
1. the browser sends a request
GET/index.html HTTP/1.1

Response from the server:
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Content-Length: 119
Content-Type: text/html

<HTML>
<Head>
<LINK rel = "stylesheet" href = "index.css">
</Head>
<Body>

</Body>
</Html>

2. the browser sends a request
Get.index.css HTTP/1.1

Response from the server:
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 70
Content-Type: text/Plane

H3 {
Font-size: 20px;
Font-weight: bold;
Color: # 005a9c;
}

3. the browser sends a request
Get image/logo.png HTTP/1.1

Response from the server:
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 1280
Content-Type: text/Plane

{Binary image data follows}

(Appendix)
1. HTTP specification: RFC published by the Internet Engineering Development Organization (IETF) specifies Internet standards, which are widely accepted by Internet research and development institutions. Because they are standard documents, they are generally written in formal languages, like the legislative document.

2. RFC: once an RFC is proposed, it is numbered and will not be changed. When a standard is modified, a new RFC is given. As a standard, RFC is widely used on the Internet.

3. Several important RFC for http:
Rfc1945 HTTP 1.0 description
Rfc2068 initial description of HTTP 1.1
Rfc2616 standard for HTTP 1.1
4. Resource Identifier uri (Uniform Resource identifter, Uri)

HTTP reference

I. Http code response code
The response code consists of three decimal digits that appear in the first line of the response sent by the HTTP server.

There are five types of response codes, represented by their first digit:
1.1xx: information, request received, continue processing
2.2xx: Successful, accepted, understood, and accepted
3.3xx: redirection. actions that must be performed further to complete the request
4.4xx: client error. The request contains a syntax error or the request cannot be implemented.
5.5xx: server error. The server cannot implement an obviously invalid request.

The following table shows each response code and its meaning:
100 continue
101 group Exchange Association
200 OK
201 created
202 accepted
203 unauthorized information
204 NO content
205 reset content
Part 1
300 multiple options
301 permanent transfer
302 found
303 see other
304 not modified
305 use proxy
307 temporary redirection
400 Error request
401 unauthorized
402 payment required
403 Forbidden
404 not found
405 methods not allowed
406 not accepted
407 proxy authorization required
408 request timeout
409 conflict
410 expired
411 Length
412 precondition is not true
413 the request instance is too large
414 the request URI is too large
415 unsupported media types
416 request range not met
417 failure expectation
500 Internal Server Error
501 unused
502 Gateway error
503 unavailable services
504 gateway timeout
505 HTTP Version Not Supported

Ii. HTTP Header
The header is composed of primary key/value pairs. They describe the properties of the client or server, the transferred resources, and the connection to the server.

Four different types of header labels:
1. General header: it can be used for requests or responses. It is associated with transactions as a whole rather than a specific resource.
2. Request Header: allows the client to pass information about itself and the desired response form.
3. Response Header: the server and the response that transmits its own information.
4. Object Header: defines the information of the transferred resource. Can be used for requests or responses.

Header Format: <name >:< value> <CRLF>

The following table describes the header labels used in HTTP/1.1.
Accept defines the media types that can be processed by the client, sorted by priority;
You can define multiple types and use wildcards in a comma-separated list. For example: accept: image/JPEG, image/PNG ,*/*
Accept-charset
Define character sets that can be processed by the client, sorted by priority;
You can define multiple types and use wildcards in a comma-separated list. Example: Accept-charset: iso-8859-1, *, UTF-8

Accept-encoding defines the encoding mechanism that the client can understand. Example: Accept-encoding: gzip, compress
Accept-language defines the natural language list that the client is willing to accept. Example: Accept-language: En, de

Accept-ranges
A response header that allows the server to specify that the request will be accepted as a resource component at a given offset and length.
The value of this header is considered as the measurement unit of the Request range. For example, accept-ranges: bytes or accept-ranges: nonea

Age allows the server to specify the length of time that has elapsed since the server generated the response, in seconds.
This header is mainly used to cache responses. Example: Age: 30

Allow is a response header that defines a list of HTTP methods supported by the secondary source in the request URI. Example: Allow: Get, put

Authorization
A response header is used to define the authorization required to access a resource (domain and encoded user ID and password ).
Example: Authorization: Basic yxv0ag9yonboaww =

Cache-control a general header used to define cache commands. Example: cache-control: Max-age = 30
Connection indicates whether to save the socket connection as an open general header. For example, connection: close or connection: keep-alive

Content-Base
An object header that defines the basic URI. In order to parse the relative URLs within the object range.
If the content-base header is not defined to parse relative URLs, use the content-location uri (exists and is absolute) or use the URI request.
For example, content-base: http: // www.myweb.com

Content-encoding is a media type modifier that specifies how an object is encoded. Example: Content-encoding: Zip
Content-language is used to specify the natural language type of data in the input stream. For example, content-language: En
Content-Length specifies the length of bytes contained in the data in the request or response. Example: Content-Length: 382

Content-location
Specify the resource location (URI) contained in the request or response ).
For example. The URL is also the starting point of the relative URL of the object to be parsed.
Example: Content-location: http://www.myweb.com/news

Content-MD5
An MD5 Digest of an object used as a Checksum.
Both the sender and receiver calculate the MD5 Digest, and the value calculated by the receiver is compared with the value passed in the header.
Example: Content-MD5: <base64 of 128 MD5 Digest>

Content-Range
It is sent along with some entities, indicating the offset between the low and high bytes of the inserted bytes, and the total length of the object.
Example: Content-range: 1001-2000/5000

Contern-type indicates the MIME type of the sent or received object. For example, Content-Type: text/html
The date on which the HTTP message is sent. Example: Date: Mon, 10pr 18:42:51 GMT

Etag is an entity header that assigns a unique identifier to the sent resource.
For resources that can use multiple URL requests, etag can be used to determine whether the actually sent resource is the same resource.
Example: etag: "208f-419e-30f8dc99"

Expires specifies the object validity period. Example: expires: Mon, 05 Dec 2008 12:00:00 GMT
Form is a request header that specifies the email address of the manual user who controls the user proxy. Example: From: webmaster@myweb.com
Host Name of the requested resource. This domain is mandatory for requests that use HTTP/1.1. Example: Host: www.myweb.com

If-modified-since
If a GET request is included, the request is conditionally dependent on the last modification date of the resource.
If the header mark is displayed and the resource has been modified since the specified date, a 304 response code should be returned.
Example: If-modified-since: Mon, 10pr 18:42:51 GMT

If-Match: if a request is contained, one or more object tags are specified. Only the etag of the instance is sent to the resource marked with a partition in the list.
Example: If-Match: "208f-419e-308dc99"

If-None-match
If a request is contained, one or more object tags are specified. The resource's etag does not match any of the conditions in the list before the operation is executed.
Example: If-None-match: "208f-419e-308dc99"

If-Range
Specifies an object tag of a resource. The client already owns a copy of the resource. Must be used with the range header.
If the object has not been modified since it was last retrieved by the client, the server sends only the specified range. Otherwise, the server sends the entire resource.
Example: range: byte = 0-499 <CRLF> If-range: "208f-419e-30f8dc99"

If-unmodified-since
This object is returned only when the requested object has not been modified since the specified date.
Example: If-unmodified-since: Mon, 10pr 18:42:51 GMT

Last-modified specifies the date and time when the requested resource was last modified. Example: Last-modified: Mon, 10pr 18:42:51 GMT
Location
A resource that has been moved is used to redirect the requester to another location.
Used with status code 302 (temporarily moved) or 301 (permanently moved.
Example: Location: http://www2.myweb.com/index.jsp

Max-forwards
A request header used for the TRACE method to specify the maximum number of proxies or gateways. This request can be routed through the gateway.
The number of proxies or gateways should be reduced before passing requests. Example: Max-forwards: 3

Pragma is a common header that sends implementation-related information. Example: Pragma: No-Cache
Proxy-Authenticate
Similar to www-authenticate, requests are authenticated only from the next server of the Request chain (proxy.
Example: proxy-Authenticate: Basic realm-Admin

Proxy-proxy-Authorization
It is similar to authorization, but it does not mean to pass anything further than in the instant server chain.
Example: proxy-authorization: Basic yxv0ag9yonboaww =

The public List displays the method sets supported by the server. Example: public: Options, mget, mhead, get, head
Range specifies the offset range of a measurement unit and a part of the requested resource. Example: range: bytes = 206-5513

Refener
A request header field that indicates the initial resource that generates the request. For an HTML form, it contains the address of the web page of the form.
Example: refener: http://www.myweb.com/news/search.html

Retry-after
A response header field, which is sent by the server in combination with status code 503 (unable to provide services) to indicate how long it should wait before the request is resumed.
This time can be a date or a unit of seconds. Example: retry-after: 18

Server indicates the header of the web server software and its version number. Example: SERVER: Apache/2.0.46 (win32)
Transfer-encoding is a common header label that indicates the type of the message body to be reversed by the recipient. Example: Transfer-encoding: chunked

Upgrade
The server is allowed to specify a new protocol or a new protocol version, which can be used together with the response code 101 (switching protocol.
Example: Upgrade: HTTP/2.0

User-Agent
Define the software type (such as a web browser) used to generate requests ).
Example: User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT; digext)

Vary is a response header label used to select a response entity from the available response representation using server-driven negotiation. Example: vary :*
A common header that contains all intermediate hosts and protocols is used to meet the request. Example: via: 1.0 fred.com, 1.1 wilma.com
Warning is used to provide the Response Header mark for response status supplement information. Example: Warning: 99 www.myweb.com piano needs tuning

www-authenticate
A User Agent is prompted to provide a response header for the user name and password, which must be used with Status Code 401 (unauthorized. Returns an Authorization header.
example: www-Authenticate: Basic realm = zxm. MGMT

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.