V. Read HTTP request header

zhaozj2021-02-16  115

5.1 HTTP Request First Overview HTTP client (eg browser), you must specify the request type when sending a request to the server (typically Get or POST). If necessary, the client can also choose to send other request heads. Most requested heads are not required, except for Content-Length. Content-length must appear for POST requests. Here are some of the most common request heads: Accept: The browser acceptable MIME type. Accept-charset: The browser acceptable character set. Accept-encoding: Browser can decode data encoding, such as Gzip. Servlets are able to return to Gzip's browsers to returns the Gzip encoded HTML page. This can reduce download time by 5 to 10 times in many cases. Accept-language: The language type of the browser wants to use when the server provides more than one language version. Authorization: Authorized information, usually appears in a response to the WWW-Authenticate header sent to the server. Connection: Indicates if a persistent connection is required. If the servlet sees the value here "Keep-alive" or see the request is HTTP 1.1 (HTTP 1.1 default persistent connection), it can take advantage of the persistent connection, when the page contains multiple elements (for example, Applet, image), significantly reduce the time you need. To achieve this, servlet needs to send a Content-Length header in your response. The simplest implementation method is: first writing the content into ByteArrayoutputStream, and then calculate its size before formal writing. Content-length: Indicates the length of the text of the request message. Cookie: This is one of the most important request headers, see the discussion in the chapter of "Cookie Processing" later. From: Request the sender's Email address, use some special web clients, the browser will not use it. Host: The host and port in the initial URL. If-modified-since: Only returns it only when the requested content is modified after the specified date, otherwise returns the 304 "Not Modified" response. Pragma: Specifies the "no-cache" value indicates that the server must return a refresh document, even if it is a proxy server and already has a local copy of the page. Referr: Contains a URL that uses the user from the page represented by the URL to access the currently requested page. User-agent: Browser type, if the content returned by servlet is very useful to the browser type. UA-PIXELS, UA-Color, UA-OS, UA-CPU: Non-standard request headers sent by some versions of IE, represent screen size, color depth, operating system, and CPU type. For complete, detailed descriptions of HTTP headers, see http specifications http://www.w3.org/protocols/. 5.2 Read the request head in the servlet is very convenient to read the HTTP header in the servlet, just call the GetHeader method of HTTPSERVLETREQUEST. If the specified header information is provided in the customer request, GetHeader returns the corresponding string; otherwise, returns NULL.

Some headers are often used, and they have a dedicated access method: getCookies method Returns the content of the cookie header. In the array of parsed, see the array of cookie objects, see the discussion of the cookie chapter; GetAuthType and getRemoteuser methods Part of the authorization header; getDateHeader and getInTheader methods read the specified header, then return the date value or integer value. In addition to reading the specified header, you can also get an Enumeration object in the request in the request in the request in GetHeadernAmes. Finally, in addition to viewing the request header information, we can also get some information from the request main command line. GetMethod method Returns a request method, requiring method usually get or post, but it is also possible to be Head, PUT or DELETE. GetRequesturi method Returns the URI (URI is part of the URL from the host and port to the form data). GetRequestProtocol returns the third part of the request command, usually "http / 1.0" or "http / 1.1". 5.3 Example: Output All request heads The following servlet instance outputs all received request heads and its values ​​in the form of a table. In addition, the servlet also outputs three parts of the main request command: request method, URI, protocol / version.

ShowRequestHeaders.java package hall; import java.io *;. Import javax.servlet *;. Import javax.servlet.http *;. Import java.util *;. Public class ShowRequestHeaders extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); String title = "Show all request headers"; out.println (ServletUtilities.headWithTitle (title) "< Body BGColor = \ "# fdf5e6\"> \N " "

" title " \N " " Request method: " Request.getMethod () "
\ N" " Request Uri: " Request.getRequesturi () "
\N" " Request Protocol: " request.getProtocol () "

\n" " \n" " \n" "< TH> Header Nam E
Header Value "); Enumeration Headernames = Request.getHeadernages (); while (head (headernames.hasmorelements ()) {string headername = (String) Headernames.nexTelement (); out.println ("

转载请注明原文地址:https://www.9cbs.com/read-8998.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.040, SQL: 9