Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use and testing of HTTP long connection and short connection

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Use Settin

We all take the HTTP1.1 protocol as an example for the settings here.

Set up HTTP short connection

Setting Connection:close in the header field closes the connection after a request / response.

Set HTTP persistent connection, which has expiration time.

Set Connection:keep-alive and Keep-Alive: timeout=60 in the header field to indicate that after the connection is established, after the idle time exceeds 60 seconds, it will expire. If you use this connection again when you are idle for 58 seconds, the connection is still valid, and after you finish using it, re-count it and expire after 60 seconds of idle.

Set HTTP persistent connection with no expiration time

Only Connection:keep-alive is set in the header field, indicating that the connection is permanent.

Realization principle

Once you know how to set it up, you start to use it. However, here comes the problem. When Connection:keep-alive is set in the request header, why is the connection disconnected after a period of idle time? This is because the connection field is valid only for server settings.

HTTP operations occur in request / response pairs, that is, the client sends the request first, and then the server processes the request. Therefore, the end operation of a HTTP operation is on the server, and the shutdown is also initiated by the server.

Next let's do the test, as well as show code. The following tests are done using Spring RestTemplate, encapsulated apache http client. In order to facilitate the explanation of the code, first explain the situation of long connection, and then make a test summary of other forms.

The client connection failure time is greater than the server failure time.

Below, for the request log. The client sets Connection: Keep-Alive and Keep-Alive: timeout=60, and the server sets Connection: Keep-Alive and Keep-Alive: timeout=5.

# # client setting validity period is 60s [2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "POST / adx-api/api/creative/upload HTTP/1.1 [\ r] [\ n]" [2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Accept: application/json, application/*+json, text/html, application/json Text/javascript [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" Content-Type: application/json Charset=UTF-8 [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML Like Gecko) Chrome/31.0.1650.16 Safari/537.36 [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" Accept-Encoding: gzip Deflate [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" Accept-Language: zh-CN [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" Connection: keep-alive [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG ] (org.apache.http.wire:?)-http-outgoing-0 > > "Keep-Alive: timeout=60 [\ r] [\ n]" [2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Content-Length: 396 [\ r] [\ n]" [2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Host: Bizdomain [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" [\ r] [\ n] "[2017-04-26 14:08:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > >" request data "# # Server settings are valid for 5s [2017-04-26 14:08:00 DEBUG] (org.apache) .http.wire:?)-http-outgoing-0 http://bizdomain:80][total kept alive: 0 Route allocated: 0 of 32; total allocated: 0 of 200] [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?)-Connection leased: [id: 0] [route: {}-> http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32 Total allocated: 1 of 200] [2017-04-26 10:57:00 DEBUG] (org.apache.http.impl.execchain.MainClientExec:?)-Opening connection {}-> http://bizdomain:80## client sets timeout 60s [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Connection: keep-alive [\ r] [\ n]" [2017-04-26 10:57 : 00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Keep-Alive: timeout=60 [\ r] [\ n]" # # Server sets the timeout 5s [2017-04-26 10:57:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 http://bizdomain:80][total kept alive: 1 Route allocated: 1 of 32; total allocated: 1 of 200] # # Keep-Alive: timeout=60 non-first request [2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?)-Connection request: [route: {}-> http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32 Total allocated: 1 of 200] # # connection expired 5s after the last request ended [2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.CPool:?)-Connection [id:2] [route: {}-> http://bizdomain:80][state:null] expired @ Wed Apr 26 14:10:05 GMT+08:00 2017

The client sets the expiration time, while the server setting does not fail

# # client sets 30s timeout [2017-04-26 17:45:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Connection: keep-alive [\ r] [\ n]" [2017-04-26 17:45:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0 > > "Keep-Alive: timeout=30 [\ r] [\ n]" # # Server sets permanent connection [ 2017-04-26 17:45:00 DEBUG] (org.apache.http.wire:?)-http-outgoing-0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report