.NET Core 2.2 HttpClient/WebClient对比Curl请求部分服务器速度过慢
I've been testing HTTP requests on Ubuntu 18.04 (.NET Core 2.2.203) and Windows 10, and noticed that .NET Core's HttpClient/WebClient is significantly slower than curl or lynx when accessing some servers—with speed differences ranging from 2x up to 50x.
Test Setup
- I used the recommended static HttpClient (also tested the
using()disposable pattern, which yielded the same slow results) - Timing implementation for .NET code:
sw.Start(); // Stopwatch timing client.GetAsync(url).Result(); sw.Stop(); - Comparison commands for native tools:
- Curl:
time curl -L "url" > /dev/null - Lynx:
time lynx "url" > /dev/null
- Curl:
Troubleshooting Attempts
I've tried every fix I could find via search, but none resolved the slowdown except wrapping curl via CurlThin:
- Disabled proxy in HttpHandler:
UseProxy = false, Proxy = null - Replaced
.Result()withawait(proper async/await pattern) - Switched to WebClient instead of HttpClient
- Tried the ModernHttpClient library
- Only curl wrappers like CurlThin matched the normal speed of native curl/lynx; all other .NET native approaches remained extremely slow.
This issue also reproduces with PowerShell's Invoke-WebRequest on Windows.
Reproduction Code & Scripts
.NET Repro Code
Run with: dotnet run -- https://reddit.com
using System; using System.Diagnostics; using System.Net.Http; namespace Download.Playground { class Program { static HttpClient client; static void Main(string[] args) { HttpClientHandler hch = new HttpClientHandler(); hch.Proxy = null; hch.UseProxy = false; client = new HttpClient(hch); Stopwatch sw = new Stopwatch(); sw.Start(); var result = client.GetAsync(args[0]).Result; sw.Stop(); Console.WriteLine($"Spent {sw.ElapsedMilliseconds}ms"); } } }
Benchmark Script
Run with: ./runbench https://reddit.com
#!/bin/bash for i in {1..20} do dotnet run -- $1 time curl -L $1 > /dev/null done
Has anyone encountered this exact issue? I've exhausted all documented fixes I could find, but nothing gets .NET's native HTTP clients to perform on par with curl/lynx for these servers.
内容的提问来源于stack exchange,提问作者CharlesS

