使用 webclient (c#) 下载文件时出现异常
Exception when downloading file using webclient (c#)
我正在使用 C# 代码从网站下载文件。我正在使用网络客户端 class:
using (var client = new WebClient())
{
client.DownloadFile(
@"http://www.cftc.gov/files/dea/history/com_disagg_txt_2018.zip",
@"destination"
);
}
该代码在数周内都运行良好。但它在大约一周前停止工作。每当我 运行 它抛出异常的代码时说:
An existing connection was closed by the remote host. (Error code 10054)
我在想也许网站开始只允许通过浏览器下载所以我补充说:
client.Headers["User-Agent"] ="Mozilla/5.0 (Windows NT 6.3; rv:36.0)
Gecko/20100101 Firefox/36.0";
但是,并没有解决问题。
有人知道解决办法吗?
这种故障很常见。
该站点已实施 TLS 1.2
协议。
The Protocol is switched to Https:
even if you specified Http:
in
your URI.
您只需启用它,因为 .Net ServicePointManager
(仍然)默认为 Ssl3/Tls 1.0
。
使用以下代码进行测试:
string Url ="http://www.cftc.gov/files/dea/history/com_disagg_txt_2018.zip";
Uri URI = new Uri(Url, UriKind.Absolute);
WebClient_DownLoad(URI, FileName);
public void WebClient_DownLoad(Uri URI, string FileName)
{
using (WebClient webclient = new WebClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
webclient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
webclient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
webclient.Headers.Add(HttpRequestHeader.Accept, "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
webclient.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8");
webclient.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8");
webclient.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
webclient.Headers.Add(HttpRequestHeader.KeepAlive, "keep-alive");
webclient.UseDefaultCredentials = true;
webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(WebClient_DownloadComplete);
webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WebClient_DownloadProgress);
webclient.DownloadFileAsync(URI, FileName);
};
}
private void WebClient_DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
string Result = string.Format("Received: {0} Total: {1} Percentage: {2}",
e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
//Update the UI
Console.WriteLine(Result);
}
private static void WebClient_DownloadComplete(object sender, AsyncCompletedEventArgs e)
{
if (!e.Cancelled)
{
if (e.Error != null)
// Update the UI: transfer completed.
Console.WriteLine("Error: " + e.Error.Message);
}else{
// Update the UI: transfer Cancelled.
Console.WriteLine("Cancelled");
}
}
我正在使用 C# 代码从网站下载文件。我正在使用网络客户端 class:
using (var client = new WebClient())
{
client.DownloadFile(
@"http://www.cftc.gov/files/dea/history/com_disagg_txt_2018.zip",
@"destination"
);
}
该代码在数周内都运行良好。但它在大约一周前停止工作。每当我 运行 它抛出异常的代码时说:
An existing connection was closed by the remote host. (Error code 10054)
我在想也许网站开始只允许通过浏览器下载所以我补充说:
client.Headers["User-Agent"] ="Mozilla/5.0 (Windows NT 6.3; rv:36.0)
Gecko/20100101 Firefox/36.0";
但是,并没有解决问题。
有人知道解决办法吗?
这种故障很常见。
该站点已实施 TLS 1.2
协议。
The Protocol is switched to
Https:
even if you specifiedHttp:
in your URI.
您只需启用它,因为 .Net ServicePointManager
(仍然)默认为 Ssl3/Tls 1.0
。
使用以下代码进行测试:
string Url ="http://www.cftc.gov/files/dea/history/com_disagg_txt_2018.zip";
Uri URI = new Uri(Url, UriKind.Absolute);
WebClient_DownLoad(URI, FileName);
public void WebClient_DownLoad(Uri URI, string FileName)
{
using (WebClient webclient = new WebClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
webclient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
webclient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
webclient.Headers.Add(HttpRequestHeader.Accept, "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
webclient.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8");
webclient.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8");
webclient.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");
webclient.Headers.Add(HttpRequestHeader.KeepAlive, "keep-alive");
webclient.UseDefaultCredentials = true;
webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(WebClient_DownloadComplete);
webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WebClient_DownloadProgress);
webclient.DownloadFileAsync(URI, FileName);
};
}
private void WebClient_DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
string Result = string.Format("Received: {0} Total: {1} Percentage: {2}",
e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
//Update the UI
Console.WriteLine(Result);
}
private static void WebClient_DownloadComplete(object sender, AsyncCompletedEventArgs e)
{
if (!e.Cancelled)
{
if (e.Error != null)
// Update the UI: transfer completed.
Console.WriteLine("Error: " + e.Error.Message);
}else{
// Update the UI: transfer Cancelled.
Console.WriteLine("Cancelled");
}
}