1. HTTP short connection & long connection
The so-called short connection is to establish a connection every time a resource is requested, and the connection is closed immediately after the request is completed. Each request goes through the process of "create tcp connection -> request resource -> response resource -> release connection".
The so-called persistent connection means that a connection is established only once, and the connection is reused for multiple resource requests, and it is closed after completion.
2、实现HTTP keep-alive
(1) The client must support: the HTTP request header sent by the client needs to add the Connection: keep-alive field.
(2) The server must support: it must be able to recognize the Connection:keep-alive field, and specify the Connection:keep-alive field in the http response.
In HTTP/1.1, keep-alive is turned on by default. To turn off keep-alive, you need to specify in the HTTP request header: Connection: close
3. Want to support keep-alive when using curl to package httpclient:
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Connection:keep-alive");
After a request is completed, the client cannot disconnect immediately:
Before request: CURL * lpcurl = curl_easy_init();
After several requests are over: curl_easy_cleanup(lpcurl);
4. Using Linux test, you can capture packets to view HTTP request headers
curl -H "Connection:keep-alive" "http://127.0.0.1:8080/cloud/common/test"