HTTP request data format

Source: Internet
Author: User
Tags mime file
From: http://nicedayyep.javaeye.com/blog/89770
HTTP request data format

Most servlet programs communicate with browser customers over HTTP, which requires programmers to have a deep understanding of the basic functions of the program and the specific operations of the HTTP protocol. When learning Servlet and JSP programming, there are two points worth noting: First, you are familiar with the HTTP protocol operation process and data format, next, we need to flexibly apply relevant methods in servlet APIs to process relevant data correctly and efficiently.

1. Description of the data format of HTTP client requests
An HTTP request consists of three parts: request line, headers, and body ). The request line consists of the Request Method, request URL request-Uri, and Protocol. The request header contains multiple attributes, the data body can be considered as a text or binary file appended to the request.
The following example shows the header content of an HTTP request. The data is actually transmitted from the IE browser to the Tomcat server using the network HTTP protocol.
GET/icwork /? Search = product HTTP/1.1
Accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/vnd. MS-PowerPoint, application/vnd. MS-Excel, application/MSWord ,*. *
Accept-language: En-US
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; digext)
HOST: www.icconcept.com: 8080
Referer: http://www.yoursite.com/header.html
Connection: keep-alive
This program uses six headers, and some other headers do not appear. Let's refer to this example to illustrate the HTTP request format.
1. http request line: the request line is in the format of Method Request-Uri protocol. In the above example, "Get/icwork /? Search = pruduct HTTP/1.1 "is the request line.
2. Accept: the MIME file format that the browser or other customers can love. Servlet can judge based on it and return the appropriate file format.
3. Accept-charset: Specifies the character encoding acceptable to the browser. The default value for an English browser is a ISO-8859-1.
4. Accept-language: indicates the language type acceptable to the browser, such as en or en-us.
5. Accept-encoding: Specifies the encoding method acceptable to the browser. The encoding method is different from the file format. It aims to compress the file and speed up file transfer. The browser decodes the Web response and then checks the file format.
6. Authorization: used to identify the browser when the password mechanism is used.
7. cache-control: Set related options for storing requests by the proxy server. Generally, servlets are not used.
8. Connection: Used to tell the server whether a fixed HTTP connection can be maintained. HTTP/1.1 uses keep-alive as the default value. In this way, when the browser requires multiple files (such as an HTML file and related graphic files), you do not need to establish a connection each time.
9. Content-Type: The content type used for the table name request. You can use the getcontenttype () method of httpservletrequest.
10. COOKIE: the browser uses this attribute to send a cookie to the server. A cookie is a small data body that is stored in a browser. It can record user information related to the server or implement session functions.
11. Reverse CT: the customer's expected response status in the table.
12. From: Provides the email address of the HTTP request owner of the client.
13. HOST: The Web name and port number in the URL.
14. If-Match: used by the put method.
15. If-modified-since: the customer uses this attribute to indicate that it only needs to change the webpage after the specified date. Because the browser can use the files it stores without having to request from the server, this saves web resources. This attribute is generally not required because servlet is a dynamically generated webpage.
16. If-None-Match: the opposite operation of IF-match for the put method.
17. If-unmodified-since: opposite to if-match-since.
18. Pragma: This attribute has only one value, namely, Pragma: No-cache, indicating that if the servlet acts as a proxy server, even if it has a stored webpage, it must pass the request to the target server.
19. Proxy-authorization: This attribute is used by the proxy server, and servlet is generally unavailable.
20. Range: if the customer has some webpages, this attribute can request the remaining parts.
21. Referer: indicates the webpage URL that generates the request. For example, from the web page/icconcept/index. in JSP, click a link to the webpage/icwork/search. In the get/icwork/search request sent to the server, the Referer is http: // hostname: 8080/icconcept/index. JSP. This attribute can be used to track the websites from which Web requests come from.
22. upgrage: You can use a different protocol than HTTP/1.1 Through this attribute setting.
23. User-Agent: the name of the client browser.
24. Via: the proxy server or web channel used to record Web requests.
25. Warning: used for delivery or storage (cache) errors declared by the customer.
Supplement. Transfer-encoding:
When the length of the report style cannot be determined in advance, it is impossible to include the Content-Length Field in the header to specify the length of the report style. In this case, the transfer-encoding field must be used to determine the length of the report style.
Generally, the value of the transfer-encoding field should be chunked, indicating that chunked encoding is used to transmit the style of the report. Chunked encoding is defined in the HTTP/1.1 RFC. Therefore, all HTTP/1.1 applications should support this encoding method.
The basic method of chunked encoding is to split large data blocks into multiple small data blocks, each of which can have its own length specified.

2. The following is a servlet that analyzes and displays the header information of the customer request (tested in Tomcat ):

Java code

Code:

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util. enumeration;

Public class headerinfo extends httpservlet {
Public void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception
{
/*
Enumeration E = Req. getheadernames ();
While (E. hasmoreelements ()){
String S = (string) E. nextelement ();
System. Out. println ("header:" + S + "" + Req. getheader (s ));
}
E = Req. getattributenames ();
While (E. hasmoreelements ()){
String S = (string) E. nextelement ();
System. Out. println ("attribute:" + S + "" + Req. getattribute (s ));
}
E = Req. getparameternames ();
While (E. hasmoreelements ()){
String S = (string) E. nextelement ();
System. Out. println ("parameter:" + S + "" + Req. getparameter (s ));
}*/
Resp. setcontenttype ("text/html; charset = GBK ");
Printwriter out = resp. getwriter ();
Out. println ("<HTML> Out. println ("<body bgcolor = \" white \ "> ");
Out. println ("<center> <font color = \" #009999 \ "size = \" 4 \ "face = \" Arial \ "> ");
Out. println ("<strong> list of all headers in Servlet Request </strong> ");
Out. println ("</font> </center> ");
Out. println ("<HR> ");
Out. println ("Out. println ("<B> method: </B>" + Req. getmethod () + "<br> ");
Out. println ("<B> URI: </B>" + Req. getrequesturi () + "<br> ");
Out. println ("<B> protocol </B>" + Req. getprotocol () + "<br> ");
Out. println ("<center> Out. println ("<Table border = 1 align = center> ");
Out. println ("<tr bgcolor = '#99cee6'> <TH> name </Th> <TH> value </Th> </tr> ");
Enumeration headernames = Req. getheadernames ();
While (headernames. hasmoreelements ()){
String headername = (string) headernames. nextelement ();
Out. println ("<tr> <TD>" + headername + "</TD> <TD>" + req. getheader (headername) + "</TD> </tr> ");
}
Out. println ("</table> </body> Out. Flush ();
}
Public void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception
{
Dopost (req, resp );
}

}

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.