简体   繁体   中英

Exception when downloading file using webclient (c#)

I'm using ac# code to download files from a website. I'm using the webclient class:

using (var client = new WebClient())
{                    
    client.DownloadFile(
        @"http://www.cftc.gov/files/dea/history/com_disagg_txt_2018.zip",
        @"destination"
     );
}

The code worked fine for several weeks. But it ceased to work about a week ago. Whenever I run the code it throws an exception saying:

An existing connection was closed by the remote host. (Error code 10054)

I was thinking that perhaps the website started to allow only download via browser so I added:

client.Headers["User-Agent"] ="Mozilla/5.0 (Windows NT 6.3; rv:36.0) 
Gecko/20100101 Firefox/36.0";

However, it did not resolve the issue.

Does anybody know a solution?

This failure is quite common.
That Site has implemented TLS 1.2 protocol.

The Protocol is switched to Https: even if you specified Http: in your URI.

You simply have to enable it, since .Net ServicePointManager (still) defaults to Ssl3/Tls 1.0 .

Test it with this code:

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");
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM