Experiment on HTTP keep-alive-personal page of Huang Yihua-Open Source Chinese community
Experiment on HTTP keep-alive
2 people add to favoritesArticleI want to add my favorite to my favorites for 6 months ago (). I have read 514 times and have a total of 1 comment.
As mentioned in the previous article, persistent connections are already configured by default in http1.1. A persistent connection is established by default unless the connection is set to close. However, we know that there may still be some gaps between fact standards and textbooks, so try it on your own.
We know that three handshakes are performed when TCP establishes a connection, and the handshake starts when one party sends a syn. Download and debug Wireshark. Java implements a piece of simulated request Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Package test;
Import java. Io. ioexception;
Import org. Apache. commons. httpclient. httpclient;
Import org. Apache. commons. httpclient. httpexception;
Import org. Apache. commons. httpclient. simplehttpconnectionmanager;
Import org. Apache. commons. httpclient. Methods. getmethod;
Import org. JUnit. test;
/**
* Todo comment of testhttpclient
*
* @ Author Yihua. Huang
*
*/
Public class testhttpclient {
@ Test
Public void testheader (){
Httpclient = new httpclient (New simplehttpconnectionmanager (true ));
Httpclient. gethttpconnectionmanager (). getparams (). setsotimeout (5000 );
Httpclient. gethttpconnectionmanager (). getparams (). setconnectiontimeout (5000 );
Getmethod get = new getmethod ("http://www.dianping.com ");
Try {
Httpclient.exe cutemethod (get );
} Catch (httpexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Thread. Sleep (10000 );
} Catch (interruptedexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
Get = new getmethod ("http://www.dianping.com ");
Get. setRequestHeader ("connection", "keep-alive ");
Try {
Httpclient.exe cutemethod (get );
} Catch (httpexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
The request result is as follows. As you can see, there are two HTTP requests in Figure 1, but only one TCP connection is established, which indicates that the persistent connection is valid. The connection will be established again after 10 seconds (starting with 40 lines of code), proving that keep-alive has timed out. Then, add keep-alive: 300 to the header. The second request will be reconnected, proving that the server has configured keep-alive timeout and does not accept keep-alive: 300 headers.
Figure 1 keep-alive takes effect for the first request
Figure 2 keep-alive expires in the second request after 10 seconds
Set the connection header to close and try again. It is found that two TCP connections are established.
Figure 3 keep-alive is invalid when connection: Close is set
Summary:
Keep-alive is just the name of persistent connection in the http1.0 era. Currently, http1.1 has already defaulted that all requests are persistent, and the RFC specification is correct.
If you do not set connection: keep-alive in the header, persistent connections are still performed.
If connection: Close is set, no persistent connection is performed.
Currently, the connection expiration time is set on the server, and the keep-alive header setting timeout time is no longer valid.