The official document provided by Microsoft describes that how HttpClient class works when connecting to a specific URL.
As described in the link above,it is neccessary to make HttpClass instances static and readonly,instead of creating HttpClass instance everytime connecting.I did some investigation.
I tried to execute the following code which invoke 10 threads and each thread connect to the specific URL.As shown below,this code creates HttpClass instance evrytime connecting.
I tried to get this traffic captured in wireshark and filter captured packets by TCP Flag which is on and HTTP Status Code which is 200.I can see that TCP connection is established everytime connecting.
Next,I tried to execute the following code which makes HttpClass instance static and readonly as the official documents provided by Microsoft describes.
Take a look at the following result.Only 2 TCP connections are established,but there are 10 HTTP responses whose source tcp port are the ones above-mentioned 2 TCP connections established(54672 and 54673).You can see the TCP connections reused.
Be the first to comment