HttpWebRequest
creates a new connection for each request. Why not share one?
I've set the KeepAlive option to true.
WIN7 .net core2.1 and .net core3.0
using System;
using System.Net;
using System.Threading;
namespace Question {
static void Test() {
var uri = new Uri("https://stackoverflow.com/");
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.KeepAlive = true;
using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) {
// nothing
}
}
static void Main(string[] args) {
while (true) {
Test();
Thread.Sleep(1000);
}
}
}
Microsoft wants everyone to use HttpClient even though the HttpWebRequest interface is a lot more intuitive for most simple cases.
They only implemented HttpWebRequest in .NET Core because .NET Standard requires it. And they didn't bother to do a thorough implementation to make things like KeepAlive work properly. The HttpWebReequest in .NET Core is a thin wrapper around HttpClient, creating a new connection for each call (which is exactly how you're NOT supposed to use HttpClient).
Here is a very good article about how Http connection pooling works in the various frameworks: https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.