.NET HttpClient post over HTTPS taking twice as long as it shoud

Viewed 617

I have found some strange behavior using the .NET HttpClient that I cannot explain. I am posting a small JSON request to an API using the .NET HttpClient with PostAsyc. The post is taking twice as long to complete (~750ms) then If I use postman to call the API endpoint directly (~380ms). I have disabled the Expect100Continue, I am not using a proxy and Keep Alive is true. See code extract below. Note: The API endpoint is hosted about 350ms away in the cloud, so my response times above are mostly due to message transit times.

....    
ServicePointManager.Expect100Continue = false;    
....   
var response = await this.httpClient.PostAsync(requestUri, postContent);
var contents = await response.Content.ReadAsStringAsync();
....

Interestingly enough, when I route the HttpClient request via Fiddler it takes the same time as If I posted via Postman (~380ms)

I have taken some Wireshark captures to show the variations in what is produced over the network. I have removed the SSL handshake lines.

.Net HttpClient posting to Api endpoint (~750ms) vs .Net HttpClient posting to Api endpoint via Fiddler(~380ms)

It looks like the HttpCLient is sending two lots of data over the wire. Send-> Ack then Send-Ack hence the 2x~380ms = ~760ms, then receiving the response.

Now as I mentioned before when I route the HttpClient request via fiddler the request takes half the time. It also looks to be sending 2 lots of data over the wire, the difference this time is it is not waiting to send the 2nd lot of data.

This is what it looks like if I post directly to the API via Postman. Postman posting to API endpoint (~380ms) . Only one lot of data is sent over the wire.

Has anyone got any ideas on why the HttpCLient Post is taking twice as long as Postman or via Fiddler to complete?

When I do a GET using the .NET HttpCLient to the API it works as expected with response times of ~380ms.

0 Answers
Related