Good memory is better than bad writing 15-http protocol

Source: Internet
Author: User
Tags response code domain server


When we use tools such as HttpServletRequest and Httpservletreonse, the protocol that these tools go through is the HTTP protocol, and the HTTP protocol is also a common protocol for our daily web development. Because the HTTP protocol is well encapsulated, we tend to overlook it, but in the pursuit of performance, these basic protocols are the basis for our continued efforts.

1. HTTP Protocol

HTTP is a shorthand for the Hypertext Transfer Protocol(Hypertext Transfer Protocol), an application-layer protocol to the TCP/IP protocol that defines the process of exchanging data between a Web browser and a Web server. After the client connects to the Web server, if you want to obtain a Web resource in the Web server, you need to follow a certain communication format, the HTTP protocol is used to define the format of the client and Web server communication.

2. HTTP version of the Protocol

HTTP version of the protocol: http/1.0, http/1.1

3. HTTP1.0 The difference between the HTTP1.1 and the

In the HTTP1.0 protocol, after a client has established a connection with a Web server, only one Web resource can be obtained.

After the HTTP1.1 protocol allows a client to connect to a Web server, it obtains multiple Web resources on one connection, and one connection can transfer multiple objects .

4. HTTP Request for agreement

http The request consists of three parts: the request line, the message header, the request body

1 , the request line begins with a method symbol, separated by a space, followed by the requested URI and version of the Protocol, in the following format: Method Request-uri http-version CRLF

where method means the request, Request-uri is a Uniform Resource identifier, http-version represents the HTTP protocol version of the request, CRLF means carriage return and newline (except as the CRLF at the end, A separate CR or LF character is not allowed.)

Request method and Explanation:

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

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

DELETE requesting the server to delete the resource identified by Request-uri

TRACE Request Server Loopback received request information, primarily for testing or diagnostics

CONNECT reserved for future use

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

Application Examples:

GET method: The browser uses the Get method to get resources to the server when the Web page is entered in the browser's address bar, eg:get/form.html http/1.1 (CRLF)

POST The method requires the requested server to accept the data appended to the request and is often used to submit the form.

eg : post/reg.jsp http/(CRLF)

Accept:image/gif,image/x-xbit,... (CRLF)

...

HOST:www.guet.edu.cn (CRLF)

Content-length:22 (CRLF)

Connection:keep-alive (CRLF)

Cache-control:no-cache (CRLF)

(CRLF)// The CRLF indicates that the message header has ended, preceded by a message header

User=name&pwd=mima// The following line is the submitted data

HEAD The method is almost the same as the get method, and for the response part of the head request, the information contained in the HTTP header is the same as the information obtained through the GET request. Using this method, you can obtain information about the resources identified by Request-uri without transmitting the entire resource content. This method is commonly used to test the validity of hyperlinks, whether they can be accessed, and whether they have been updated recently.

5. HTTP response of the Protocol

after receiving and interpreting the request message, the server returns an HTTP response message.

HTTP The response is also made up of three parts, namely: status line, message header, response body

1 , the status line format is as follows:

Http-version Status-code Reason-phrasecrlf

where Http-version represents the version of the server HTTP protocol, Status-code represents the response status code sent back by the server, and Reason-phrase represents a textual description of the status code.

The status code consists of three digits, the first number defines the category of the response, and there are 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 -- further action is required to complete the request

4xx : Client Error-- request syntax error or request not implemented

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

Common status codes, status descriptions, descriptions:

$ OK// Client Request succeeded

Request// client requests have syntax errors and cannot be understood by the server

401 Unauthorized// request is not authorized, this status code must be used with the Www-authenticate header field

403 Forbidden// the server received the request but refused to provide the service

404 Not Found// Request resource does not exist, eg: The wrong URL was entered

Internal Server Error// Unexpected error occurred on server

503 Server unavailable// the server is currently unable to process client requests and may return to normal after some time

eg : http/1.1 OK(CRLF)

Common response headers in HTTP responses (message headers)

Location : The server goes through this header to tell the browser where to jump

server: Servers through this header, tell the browser server model

content-encoding: The server through this header, tells the browser, the data compression format

content-length: The server passes this header and tells the browser the length of the loopback data

content-language: The server uses this header to tell the browser the locale

content-type: The server passes this header and tells the browser the type of loopback data

Refresh: The server passes this header and tells the browser to refresh periodically

content-disposition: The server through this header, tell the browser to download the way to hit the data

transfer-encoding: The server uses this header to tell the browser that the data is being echoed in chunks

Expires:-1 Control browser do not cache

Cache-control:no-cache

Pragma:no-cache

6. HTTP Protocol Header Description (appendix)

HTTP A message consists of a client-to-server request and a server-to-client response. Both the request message and the response message are from the start line (for the request message, the start line is the request line, for the response message, the start line is the status line), the message header (optional), the empty line (only the CRLF line), and the message body (optional) is composed.

HTTP The message header includes the normal header, the request header, the response header, and the entity header.

Each header fieldconsists of a name + ":" + a Space + value, and the name of the message header field is case-insensitive.

1 , normal header

In the normal header, a small number of header fields are used for all request and response messages, but not for the transferred entity, only for the transmitted messages.

Cache-control used to specify the cache instruction, the cache instruction is unidirectional (the cache instruction appearing in the response may not appear in the request), and is independent (the cache instruction of one message does not affect the caching mechanism of another message processing), and HTTP1.0 uses a similar header domain of pragma.

cache directives at request include: No-cache(used to indicate that the request or response message cannot be cached), No-store, Max-age, Max-stale, Min-fresh, only-if-cached;

cache directives for response include:public, Private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age, S-maxage.

eg : In order to instruct IE Browser (client) not to cache the page, the server-side JSP program can be written as follows: Response.sehheader ("Cache-control", "No-cache");

//response.setheader ("Pragma", "No-cache"); function equivalent to the above code, usually both// shared

This code will set the normal header field in the Sent response message: Cache-control:no-cache

Date The normal header field indicates the date and time that the message was generated

Connection The normal header field allows the option to send a specified connection. For example, specify that the connection is contiguous, or specify a "close" option to notify the server to close the connection after the response is complete

2 , request header

The request header allows the client to pass additional information about the request to the server side, as well as the client itself.

Common Request Headers

Accept the request header field is used to specify which types of information the client accepts. eg: Accept:image/gif, indicating that the client wants to accept a resource in GIF image format; accept:text/html, indicating that the client wants to accept HTML text.

Accept-charset the request header field is used to specify the character set accepted by the client. eg: accept-charset:iso-8859-1,gb2312. If you do not set this field in the request message, the default is that any character set is acceptable.

accept-encoding The request header field is similar to accept, but it is used to specify acceptable content encoding. Eg:accept-encoding:gzip.deflate. If the domain server is not set in the request message, the client is assumed to be acceptable for various content encodings.

Accept-language The request header field is similar to accept, but it is used to specify a natural language. EG:ACCEPT-LANGUAGE:ZH-CN. If the header field is not set in the request message, the server assumes that the client is acceptable for each language.

Authorization The request header domain is primarily used to prove that a client has permission to view a resource. When a browser accesses a page, if the response code of the receiving server is 401(not authorized), a request containing the authorization request header domain can be sent, requiring the server to validate it.

Host (the header field is required when the request is sent)

Host The request header domain is primarily used to specify the Internet host and port number of the requested resource, which is usually extracted from the HTTP URL.

3 , response header

The response header allows the server to pass additional response information that cannot be placed in the status line, as well as information about the server and the next access to the resources identified by Request-uri.

Common response Headers

Location The response header field is used to redirect the recipient to a new location. location response header fields are commonly used when changing domain names.

Server The response header field contains the software information that the server uses to process the request. corresponds to the User-agent request header field. Below is

Server An example of a response header field:

Server : apache-coyote/1.1

Www-authenticate

www-authenticate The response header domain must be included in the 401(unauthorized) response message, when the client receives a 401 response message and sends the authorization header domain to the request server to validate it, and the service-side response header contains the header domain.

eg  : Www-authenticate:basic realm= "Basic authtest!" you can see that the server is using a Basic authentication mechanism for the requested resource.

4 , Entity header

both request and response messages can send an entity. An entity consists of an Entity header field and an entity body, but it does not mean that the entity header fields and entity bodies are sent together, and only the entity header fields can be sent. The entity header defines the meta-information about the entity body (eg: there is no entity body) and the resource identified by the request.

Common entity Headers

content-encoding The Entity header field is used as a modifier for the media type, and its value indicates the encoding of additional content that has already 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 This method of compressing the document, Eg:content-encoding:gzip

Content-language

Content-language The Entity header field describes the natural language used by the resource. The domain is not set and the entity content is considered to be available to all language readers.

Content-length The Entity header field is used to indicate the length of the entity body, expressed as a decimal number stored in bytes.

Content-type The Entity Header field term indicates the media type that is sent to the recipient's entity body. eg:

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

content-type:text/html;charset=gb2312

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

Expires The Entity header field gives the date and time when the response expires. In order for a proxy server or browser to update the cache after a period of time (once again accessing pages that have been visited, loading directly from the cache, shortening response times, and reducing server load), we can use the Expires entity header domain to specify when the page expires. eg:expires:thu,15 SEP 2006 16:23:12 GMT

HTTP1.1 the client and cache must treat other illegal date formats (including 0) as expired. Eg: in order to let the browser do not cache the page, we can also take advantage of the Expires entity header domain, set as 0,jsp in the program as follows: Response.setdateheader ("Expires", "0");

Good memory is better than bad writing 15-http protocol

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.