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

使用C#下载网页源码时遭遇远程主机强制关闭连接错误

Fixing "An existing connection was forcibly closed by the remote host" in C# WebClient

Hey there, I’ve run into this exact frustrating issue before when pulling web content with WebClient! That error pops up when the remote server actively cuts off your connection—usually because it doesn’t recognize your request as a legitimate browser session, or there’s a mismatch in security settings. Let’s walk through the most effective fixes:

1. Spoof browser request headers

Most modern servers block requests that lack proper browser-like headers. Add these to your WebClient to mimic a real Chrome browser:

var urlcli = new WebClient();
// Add essential headers to avoid being flagged as a bot
urlcli.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
urlcli.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
urlcli.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");

string sourcecode = urlcli.DownloadString(urltxt.Text.Trim());

WebClient is a legacy class—HttpClient is more robust, handles connections better, and supports modern web standards. Here’s how to use it:

// Use 'using' to properly dispose the client
using var httpClient = new HttpClient();
// Set user agent header
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");

// Async version (preferred for non-blocking calls)
string sourcecode = await httpClient.GetStringAsync(urltxt.Text.Trim());

// If you need synchronous code (not ideal but works)
// string sourcecode = httpClient.GetStringAsync(urltxt.Text.Trim()).Result;

3. Force modern TLS versions

Many servers no longer support older TLS protocols (like TLS 1.0/1.1). Tell your app to use TLS 1.2 or 1.3 before making the request:

// Set this at the start of your method or application initialization
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

var urlcli = new WebClient();
// Rest of your code...

4. Increase timeout duration

Sometimes the connection gets closed because the server takes longer to respond than WebClient’s default timeout. Extend it:

var urlcli = new WebClient();
urlcli.Timeout = 15000; // 15 seconds (adjust as needed)
// Add headers/TLS settings...
string sourcecode = urlcli.DownloadString(urltxt.Text.Trim());

5. Verify the URL format

Double-check that your URL includes the full scheme (http:// or https://). Some servers force HTTPS redirects, and WebClient might not handle them correctly—try manually replacing http with https if the site supports it.

Quick note on the error

This error means the remote server actively terminated the connection. It’s rarely a problem with your code directly—usually it’s the server’s way of saying "your request looks suspicious" or "we don’t support your connection settings". The fixes above address 90% of these cases!

内容的提问来源于stack exchange,提问作者Narender Godara

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:20:19