1, what is HTTPWhen the client is connected to a Web server, if you want to obtain a Web resource in the Web server, you must follow a certain communication format, which is used to define the format of the client and Web server communications.
HTTP is a shorthand for the Hypertext Transfer Protocol (Hypertext Transfer Protocol), an application-layer protocol of the TCP/IP protocol that defines the process of exchanging data between a Web browser and a Web server.
Use the Telnet program to connect to the Web server, and use the HTTP protocol to get a page to quickly understand the HTTP protocol's role.
Telnet cannot be used: Click to open link
Install IE browser plugin httpwatch, view IE browser to get a page through the HTTP protocol.
2, the difference between HTTP1.0 and 1.1In the HTTP1.0 protocol, a client can only obtain one web resource after it has established a connection to the Web server. HTTP1.1 protocol, which allows clients to obtain multiple Web resources on one connection after establishing a connection to the Web server.
A Web page that uses an IMG tag to refer to three pictures, when the client accesses the Web page in the server, the client accesses the server a total of times, sending several HTTP requests to the server.
4 times. The entire HTML page request, then the request after parsing the three tags.
Improve server performance and reduce the number of HTTP requests. Multiple pictures, Css,javascript merged.
3. HTTP requestAfter the client connects to the server, a Web resource is requested from the server, which is called a client sending an HTTP request to the server. A complete HTTP request includes the following:
A request line, request header, and request question (form), as follows:
Request entity: generally used for forms.
4, HTTP request lineRequest mode: Mostly get POST (default is Get method). Can be modified by means of form form
Whether post or get is used to request a Web resource from the server, the difference between the two approaches is mainly reflected in data delivery:
(1), such as the request mode is get way, then can be in the form of the requested URL to bring the data to the server, a number of data separated by &, such as: Get/mail/1.html?name=abc&password=xyz HTTP /1.1 <a href= "a/2.html?name=aaa&password=123" > Dot </a> Get mode features: The parameters that come with the URL address are limited, and their data capacity is usually not more than 1K. Cannot be Chinese (2), if the request mode is post mode, you can send data to the server in the requested entity content, the Post method features: The amount of data transmitted is unrestricted content in the entity content.
5. HTTP request HeadersAccept: The browser tells the server through this header that it supports the data type accept:text/html,image/* Accept-char Set: The browser tells the server through this header which character set Accept-charset:iso-8859-1 encoding method is supported UTF-8
Accept-encoding: The browser tells the server through this header, supported compression format accept-encoding:gzip,compress
Accept-language: The browser tells the server through this header that its language environment ACCEPT-LANGUAGE:EN-US,ZH-CN
Host: The browser through this header to tell the server, want to access which host host:www.it315.org:80
If-modified-since: The browser tells the server through this header, the time to cache the data If-modified-since:tue 2000 18:23 : Wuyi GMT
Referer: The browser uses this header to tell the server which page the client is fromanti-theft chainreferer:http://www.it315.org/index.jsp
Connection: The browser through this head to tell the server, after the request is disconnected or what is the link connection:close/keep-alive
6, HTTP responseAn HTTP response represents the data that the server sends to the client, including:
A status line, several message headers, and the entity content.
7, HTTP status line
100-199 not commonly used 200-299 success tips, commonly used 200 302 means to find another server to take it (I have no money to find someone to borrow, and 307 similar) 304 said Don't look for me, find a buffer. 400-499 indicates a problem with the client, common 404 500 Server Error
8. HTTP Status HeaderLocation: Server through this header, to tell the browser where to jump to match 302,location:http://www.it315.org/ind ex.jsp
Server: Servers through this header, tell the browser server model Server:apache Tomcat
Content-encoding: The server through this header, tells the browser, the data compression format Content-encoding:gzip
Content-length: The server uses this header to tell the browser the length of the loopback data content-length:80
Content-language: Server Through this header, tell browser locale CONTENT-LANGUAGE:ZH-CN
Content-type: The server through this header, tells the browser loopback data type content-type:text/html; charset=gb2312
Refresh: Server Through this header, tell the browser to refresh regularly (chat room) refresh:1;url=http://www.it315.org
Content-disposition: Server Through this header, tell the browser to download the way to play data content-disposition:attachment; Filename=aaa.zip
Transfer-encoding: The server uses this header to tell the browser that the data is echoed in chunks transfer-encoding:chunked
Expires:-1 Control browser do not cache (also can write time, cache to when) Expires:-1
Cache-control:no-cache
Pragma:no-cache
Zip Code:
Data compression public
void Test2 (HttpServletRequest request, httpservletresponse response) throws ioexception{
String data = "Abcdabcdab";
SYSTEM.OUT.PRINTLN ("Raw data size is:" + data.getbytes (). length);
Bytearrayoutputstream bout = new Bytearrayoutputstream ();
Gzipoutputstream gout = new Gzipoutputstream (bout); Buffer
Gout.write (data.getbytes ());
Gout.close ();
Gets the compressed data
byte g[] = Bout.tobytearray ();
Response.setheader ("content-encoding", "gzip");
Response.setheader ("Content-length", G.length + "");
Response.getoutputstream (). write (g);
}
Loopback picture data:
Specifies the loopback data type public
void Test3 (HttpServletRequest request, httpservletresponse response) throws ioexception{
// C:\apache-tomcat-6.0.20\conf\web.xml
response.setheader ("Content-type", "image/jpeg");
InputStream in = This.getservletcontext (). getResourceAsStream ("/1.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = Response.getoutputstream ();
while ((Len=in.read (buffer)) >0) {
out.write (buffer, 0, Len);
}
}
Timed refresh:
Specifies that the browser periodically refreshes
the public void test4 (HttpServletRequest request, httpservletresponse response) throws ioexception{
response.setheader ("Refresh", "3;url= ' http://www.sina.com '");
Response.getwriter (). Write ("ABCD");
Specify download:
Specify browser download public
void Test5 (HttpServletRequest request, httpservletresponse response) throws
ioexception{ Response.setheader ("Content-disposition", "attachment;filename=xxx.jpg");
InputStream in = This.getservletcontext (). getResourceAsStream ("/2.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = Response.getoutputstream ();
while ((Len=in.read (buffer)) >0) {
out.write (buffer, 0, Len);
}
}