You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 9控制台程序中HttpClient请求IMF直播页面时挂起或报错的问题求助

.NET 9控制台程序中HttpClient请求IMF直播页面时挂起或报错的问题求助

我正在尝试用代码请求IMF的直播页面 https://www.imf.org/en/live,使用的是.NET 9控制台应用,具体代码如下:

using var httpClientHandler = new HttpClientHandler() { MaxAutomaticRedirections = 10, AllowAutoRedirect = true};
using var httpClient = new HttpClient(httpClientHandler);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "https://www.imf.org/en/live");    

httpRequest.Headers.TryAddWithoutValidation("authority", "www.imf.org");
httpRequest.Headers.TryAddWithoutValidation("method", "GET");    
httpRequest.Headers.TryAddWithoutValidation("scheme", "https");
httpRequest.Headers.TryAddWithoutValidation("accept", "text/html");
httpRequest.Headers.TryAddWithoutValidation("accept-encoding", "gzip");
httpRequest.Headers.TryAddWithoutValidation("accept-language", "en-US,en;q=0.9");
httpRequest.Headers.TryAddWithoutValidation("cache-control", "no-cache");
httpRequest.Headers.TryAddWithoutValidation("pragma", "no-cache");

var response = await httpClient.SendAsync(httpRequest);
response.EnsureSuccessStatusCode();

但运行这段代码时,程序要么卡住,要么抛出以下错误:

{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.."}

有意思的是,我用PowerShell发送完全相同的请求头和请求时,却能正常工作并返回内容,PowerShell代码如下:

Invoke-WebRequest -UseBasicParsing -Uri "https://www.imf.org/en/live" `
-Headers @{  
  "authority"="www.imf.org"
  "method"="GET" 
  "scheme"="https"
  "accept"="text/html"
  "accept-encoding"="gzip"
  "accept-language"="en-US,en;q=0.9"
  "cache-control"="no-cache"
  "pragma"="no-cache"    
}

返回的结果正常,能得到页面的HTML内容片段:

StatusCode        : 200
StatusDescription : OK
Content           :


                    <!DOCTYPE html>
                    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

我已经花了好几个小时调整HttpClientHandler和SocketsHttpHandler的配置参数,但都没解决问题。有没有大佬知道这是为什么呀?

备注:内容来源于stack exchange,提问作者Fernando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:47:57