HTTP protocol Learning

Source: Internet
Author: User

Characteristics

The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
3. Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
4. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.

1. Request Header

  The most common request headers for HTTP are as follows:

  Accept: The MIME type acceptable to the browser;

  Accept-charset: The acceptable character set of the browser;

  accept-encoding: The way the browser can decode data encoding, such as gzip. The servlet can return a GZIP-encoded HTML page to a browser that supports gzip. In many cases this can reduce the 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, which usually appears in the response to the 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 an HTTP 1.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;

  Cookie: 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, which is not used by the browser;

  Host: The hosts and ports in the initial URL;

  if-modified-since: Returns a 304 "not Modified" answer only if the requested content has been modified after the specified date;

  Pragma: Specifying a value of "No-cache" indicates 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 from which 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: A nonstandard request header sent by some versions of Internet Explorer to indicate screen size, color depth, operating system, and CPU type.

2. Response header

The most common response headers for HTTP are as follows: l

  allow: Which request methods are supported by the server (such as GET, post, etc.);

content-encoding: The Encoding (Encode) method of the document. The content type specified by the Content-type header can be obtained only after decoding. Using gzip to compress documents can significantly reduce the download time of HTML documents. Java's gzipoutputstream can be easily gzip compressed, but only on Unix Netscape and IE 4, ie 5 on Windows. Therefore, the servlet should check whether the browser supports gzip by looking at the accept-encoding header (that is, Request.getheader ("accept-encoding")). Returns the gzip-compressed HTML page for a browser that supports gzip, returning a normal page for another browser;

content-length: Indicates the content length. This data is only required if the browser is using a persistent HTTP connection. If you want to take advantage of the persistent connection, you can write the output document to Bytearrayoutputstram, look at its size when done, then put that value into the Content-length header and finally pass the Bytearraystream.writeto ( Response.getoutputstream () send content; l

Content-type: Indicates what MIME type the following document belongs to. The servlet defaults to Text/plain, but it usually needs to be explicitly specified as text/html. Because Content-type is often set up, HttpServletResponse provides a dedicated method Setcontenttyep. The corresponding relationship between the extension and MIME type can be configured in the Web. xml file;

Date: The current GMT time. You can use Setdateheader to set this head to avoid the hassle of converting the time format;

Expires: Indicates when the document should be assumed to have expired and thus no longer caches it. L

last-modified: The last time the document was changed. The customer can provide a date through the If-modified-since request header, which is treated as a conditional get, and only documents that have been modified later than the specified time are returned, otherwise a 304 (not Modified) state is returned. Last-modified can also be set using the Setdateheader method;

Location: Indicates where the customer should go to extract the document. Location is usually not set directly, but through HttpServletResponse's Sendredirect method, which simultaneously sets the status code to 302;

Refresh: Indicates how much time the browser should refresh the document, in seconds. In addition to refreshing the current document, you can also pass SetHeader ("Refresh", "5; Url=http://host/path ") lets the browser read the specified page. Note This functionality is usually done by setting the HTML page in the head area of the <meta http-equiv= "Refresh" content= "5; Url=http://host/path "> is implemented because automatic refresh or redirection is important for HTML writers who cannot use CGI or servlets. For Servlets, however, it is more convenient to set the refresh header directly. Note that the meaning of refresh is "refresh this page after n seconds or visit the specified page" instead of "refresh this page every n seconds or visit the specified page". Therefore, continuous refresh requires a refresh header to be sent each time, and sending a 204 status code prevents the browser from continuing to refresh, whether it is using the refresh header or the <meta http-equiv= "Refresh" ...>. Note that the refresh header is not part of the HTTP 1.1 formal specification, but rather an extension, but both Netscape and IE support it.

HTTP Status Code list:

Status code

Status Code English name

Chinese description

100

Continue

Go on. The client should continue its request

101 Switching protocols Switch protocols. The server switches the protocol based on the client's request. You can only switch to a more advanced protocol, for example, a new version of the protocol that switches to HTTP

200

Ok

The request was successful. Typically used for Get and post requests

201

Created

has been created. A new resource was successfully requested and created

202

Accepted

has been accepted. The request has been accepted, but the completion is not processed

203

Non-authoritative Information

Non-authorized information. The request was successful. But the meta information returned is not on the original server, but a copy

204

No Content

No content. The server processed successfully, but the content was not returned. Make sure the browser continues to display the current document without updating the page

205

Reset Content

Resets the content. The server processing succeeds, and the user terminal (for example: browser) should reset the document view. The browser's form fields can be cleared by this return code

206

Partial Content

Part of the content. The server successfully processed a partial GET request

300

Multiple Choices

Multiple options. The requested resource can include multiple locations, which can return a list of resource characteristics and addresses for use in the user terminal (for example: browser) selection

301

Moved Permanently

Move permanently. The requested resource has been permanently moved to the new URL, the return information will include the new URL, and the browser will automatically be directed to the new URL. Any future new requests should be replaced with a new URL

302

Found

Temporary move. Similar to 301. But resources are only temporarily moved. The client should continue to use the original URL

60V

See other

See other addresses. Similar to 301. Viewing with Get and post requests

304

Not Modified

Not modified. The requested resource is not modified and no resources are returned when the server returns this status code. Clients typically cache accessed resources by providing a header indicating that the client wants to return only the resources modified after the specified date

305

Use Proxy

Use a proxy. The requested resource must be accessed through the proxy

60S

Unused

HTTP status codes that have been discarded

307

Temporary Redirect

Temporary redirection. Similar to 302. REDIRECT with GET request

400

Bad Request

Client request syntax error, server does not understand

401

Unauthorized

Request for user authentication

402

Payment Required

Reserved for future use

403

Forbidden

The server understands the request from the requesting client, but refuses to execute the request

404

Not Found

The server could not find the resource (Web page) based on the client's request. With this code, the Web designer can set up a personalized page that "the resource you requested could not be found"

405

Method not allowed

The method in the client request is forbidden

50W

Not acceptable

The server cannot complete the request based on the content attributes requested by the client

407

Proxy Authentication Required

The request requires the authentication of the agent, similar to 401, but the requestor should use the proxy for authorization

408

Request time-out

The server waits for a client to send a request that is too long and times out

409

Conflict

The server may return this code when it finishes a put request from the client, and the server conflicts when it processes the request

410

Gone

The resource requested by the client no longer exists. 410 is different from 404, if the resource is now permanently deleted and can use 410 code, the site designer can specify a new location for the resource through 301 code

411

Length Required

The server cannot process request information sent by the client without content-length

412

Precondition Failed

Prerequisite error for client request information

413

Request Entity Too Large

The request was rejected because the requested entity was too large to be processed by the server. To prevent successive requests from the client, the server may close the connection. If only the server is temporarily unable to process, it will contain a retry-after response message

414

Request-urltoo Large

The requested URL is too long (URLs are usually URLs) and the server cannot process

415

Unsupported Media Type

The server cannot process the media format that was included with the request

416

Requested range not satisfiable

Invalid scope for client request

417

Expectation Failed

Server failed to satisfy expect request header information

500

Internal Server Error

Server internal error, unable to complete request

501

Not implemented

The server does not support the requested feature and cannot complete the request

40S

Bad Gateway

A server acting as a gateway or proxy that received an invalid request from the remote server

503

Service unavailable

Due to overloading or system maintenance, the server is temporarily unable to process client requests. The length of the delay can be included in the server's Retry-after header information

504

Gateway time-out

A server that acts as a gateway or proxy and does not get requests from the remote server in time

505

HTTP Version not supported

The server does not support the version of the requested HTTP protocol and cannot complete processing

Source: (Baidu Encyclopedia) https://baike.so.com/doc/5366073-5601774.html

(Blog Park--http detailed) http://www.cnblogs.com/EricaMIN1987_IT/p/3837436.html

HTTP protocol Learning

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.