HTTP Request Header Overview

Source: Internet
Author: User
HTTP Request Header Overview

 

HTTP client programs (such as browsers) must specify the request type (usually GET or POST) when sending requests to the server ). If necessary, the customer program can also choose to send other request headers. Most request headers are not required,

Except Content-Length. Content-Length must appear for POST requests.

Below are some of the most common request headers

Accept: the MIME type acceptable to the browser.

Accept-Charset: the acceptable character set of the browser.

Accept-Encoding: The data Encoding method that the browser can decode, such as gzip. Servlet can return gzip-encoded HTML pages to a browser that supports gzip. In many cases, this can reduce downloads by 5 to 10 times.

Time.

Accept-Language: the type of Language that the browser wants to use when the server can provide more than one Language version.

Authorization: Authorization information, usually displayed in the response to the WWW-Authenticate header sent by the server.

Connection: Indicates whether a persistent Connection is required. If the Servlet sees that the value here is; Keep-Alive ", or the request uses HTTP 1.1 (HTTP 1.1 performs a persistent connection by default), it can use persistent connections.

When a page contains multiple elements (such as an Applet or image), the download time is significantly reduced. To achieve this, the Servlet needs to send a Content-Length header in the response. The simplest implementation method is:

Write the content to ByteArrayOutputStream first, and then calculate its size before writing the content.

Content-Length: the Length of the Request Message Body.

Cookie: This is one of the most important request header information.

From: the e-mail address of the Request sender, which is used by some special Web client programs and not used by the browser.

Host: Host and port in the initial URL.

If-Modified-Since: the request is returned only when the requested content is Modified after the specified date. Otherwise, the system returns the 304; Not Modified "response.

Pragma: specified; no-cache "value 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 you can access the current requested page.

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 IE, indicating screen size, Color depth, operating system, and cputype.

For a complete and detailed description of the HTTP header, see the HTTP specification for the http://www.w3.org/Protocols.

8.1 HTTP Response Header Overview (HttpServletResponse)

The HTTP Response of a Web server generally consists of one status line, one or more response headers, one blank line, and content document. Setting the HTTP response header is often combined with the status code in the Set status line. For example

The status code is accompanied by a Location header, while the 401 (Unauthorized) status code must be accompanied by a WWW-Authenticate header.

However, it is useful to specify the Response Header even if the status code with special meanings is not set. The response header can be used to set the Cookie, specify the modification date, and instruct the browser to refresh the page at the specified interval.

The length of the file so that persistent HTTP connections can be used ,...... And many other tasks.

The most common method to set the response header is the setHeader of HttpServletResponse. This method has two parameters, indicating the name and value of the Response Header, respectively. Similar to the set Status Code, setting the response header should be in the process of sending any document content

Previously.

The setDateHeader and setIntHeadr methods are used to set the response headers containing dates and integers. The former avoids the trouble of converting Java time into GMT time strings, the latter avoids converting integers into strings.

Trouble.

HttpServletResponse also provides many settings ?? Why is there a problem? Filters? Jing?

SetContentType: Set the Content-Type header. Most servlets use this method.
SetContentLength: Set the Content-Length header. This function is useful for browsers that support persistent HTTP connections.
AddCookie: Set a Cookie (the Servlet API does not have the setCookie method, because the response usually contains multiple Set-Cookie headers ).
In addition, the Location header is also set when the sendRedirect method sets status code 302.

For more information about HTTP headers, see http://www.w3.org/protocols.

Response Header description
Which request methods (such as GET and POST) are supported by the Allow server ).
The Encoding (Encode) method of the Content-Encoding document. The Content Type specified by the Content-Type header can be obtained only after decoding. Gzip compression can significantly reduce the download time of HTML documents. Java

GZIPOutputStream can be easily compressed by gzip, but it is supported only by Netscape on Unix and IE 4 and IE 5 on Windows. Therefore, the Servlet should view the Accept-Encoding header (that is

Request. getHeader ("Accept-Encoding") check whether the browser supports gzip. For a browser that supports gzip, return the HTML page compressed by gzip and other normal pages.
Content-Length indicates the Content Length. This data is required only when the browser uses a persistent HTTP connection. If you want to take advantage of persistent connections, you can write the output document to ByteArrayOutputStram.

Size, put the value in the Content-Length header, and then send the Content through byteArrayStream. writeTo (response. getOutputStream.
Content-Type indicates the MIME Type of the subsequent document. Servlet is text/plain by default, but it must be explicitly specified as text/html. Because Content-Type is often set, therefore, HttpServletResponse

For a dedicated method setContentTyep.
The current GMT time of Date. You can use setDateHeader to set this header to avoid the trouble of converting the time format.
When should Expires consider that the document has expired and thus it is no longer cached?
The Last modification time of the Last-Modified document. You can use the If-Modified-Since request header to provide a date. This request is considered as a condition GET. Only documents whose modification time is later than the specified time will be returned. Otherwise

Returns a 304 (Not Modified) status. Last-Modified can also be set using the setDateHeader method.
Location indicates where the customer should extract the document. Location is usually not set directly, but through the sendRedirect method of HttpServletResponse. This method also sets the status code to 302.
Refresh indicates the time after which the browser should Refresh the document, in seconds. In addition to refreshing the current document, you can also use setHeader ("Refresh", "5; URL = http: // host/path") to allow the browser to read the specified page.

. Note that this feature is typically implemented by setting the <META HTTP-EQUIV = "Refresh" CONTENT = "5; URL = http: // host/path"> In the HEAD area of the HTML page, this is because automatic refresh or redirection cannot be used

HTML writers using CGI or Servlet are very important. However, for Servlet, it is more convenient to directly set the Refresh header. Note that Refresh indicates that Refresh the current page or access the specified page after N seconds "instead of refreshing every N seconds.

New page or access specified page ". Therefore, continuous Refresh requires that a Refresh header be sent each time, while sending the 204 status code can prevent the browser from refreshing continuously, whether it is using the Refresh header or the meta http-

EQUIV = "Refresh"...> "...〉. Note that the Refresh header is not part of the official HTTP 1.1 Specification but an extension, but is supported by both Netscape and IE.
Server name. Servlet generally does not set this value, but is set by the Web server.
Set-Cookie: Set the Cookie associated with the page. Servlet should not use response. setHeader ("Set-Cookie",...), but should use the dedicated method addCookie provided by HttpServletResponse. See the following

Cookie settings.
What type of Authorization information should the WWW-Authenticate client provide in the Authorization header? This header is required in the Response containing the 401 (Unauthorized) status row. For example, response. setHeader ("WWW-

Authenticate "," BASIC realm = \ "executives \""). Note that Servlet generally does not handle this problem, but allows the Web server to control access to password-protected pages (for example,. htaccess ).

 

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.