简体   繁体   English

循环中的 WebClient 工作一次,然后超时

[英]WebClient in loop works once, then times out

My code to download a handful of files from a web source will retrieve the files name from the server and then begin the download.我从 web 源下载一些文件的代码将从服务器检索文件名,然后开始下载。 However once it grabs the filename for the second in the queue, it stops downloading and eventually times out.然而,一旦它获取了队列中第二个文件名,它就会停止下载并最终超时。 What am I missing here?我在这里错过了什么? Any advice appreciated.任何建议表示赞赏。

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string[] s = (string[])e.Argument;

        for (int x = 0; x < s.Length; x++) {

            using (WebClient client = new WebClient())
            {
               
                client.OpenRead(s[x]);

                string header_contentDisposition = client.ResponseHeaders["content-disposition"];
                string filename = new ContentDisposition(header_contentDisposition).FileName;

                

                string filedownload = AppDomain.CurrentDomain.BaseDirectory + filename;
                var url = new Uri(s[x]);

                client.DownloadFileAsync(url, filedownload);
                client.Dispose();


                /* HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                 {

                     var wresp = (HttpWebResponse)request.GetResponse();

                     using (Stream file = File.OpenWrite(filename))
                     {
                         wresp.GetResponseStream().CopyTo(file);

                     }
                 }
                */


                //client.DownloadFileAsync(url, filedownload);



            }
        }
    }

After changing my download code several times and having similar results each time, I found adding the following:在多次更改我的下载代码并且每次都有类似的结果后,我发现添加了以下内容:

System.Net.ServicePointManager.DefaultConnectionLimit = 10;

to my form fixed the issue.我的表格解决了这个问题。 Don't ask me why that works, I'd love an explanation.不要问我为什么会这样,我想要一个解释。

Check this explanation out https://learn.microsoft.com/de-de/archive/blogs/jpsanders/understanding-maxservicepointidletime-and-defaultconnectionlimit查看此解释https://learn.microsoft.com/de-de/archive/blogs/jpsanders/understanding-maxservicepointidletime-and-defaultconnectionlimit

If the commented code is part of the code which breaks it then try closing your requests using.Close().如果注释代码是破坏它的代码的一部分,请尝试使用 .Close() 关闭您的请求。 They might get disposed of since you have them in using blocks, but it still keeps the connection active/open in the ServicePoint object and since DefaultConnectionLimit is set to 2 by default, your ServicePoint is already clogged.它们可能会被处理掉,因为您在使用块时使用了它们,但它仍然在 ServicePoint object 中保持连接处于活动状态/打开状态,并且由于 DefaultConnectionLimit 默认设置为 2,因此您的 ServicePoint 已经被阻塞。

In my project I was first getting the size of the file to display to the user using WebRequest/Response and then download the file, and that worked only once.在我的项目中,我首先使用 WebRequest/Response 获取要显示给用户的文件大小,然后下载文件,这只工作了一次。 Upon closing the Response objects, it started working properly.关闭 Response 对象后,它开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# WebClient DownloadFile 只工作一次 - C# WebClient DownloadFile works only once c#WebBrowser DocumentText只能工作一次但不能循环吗? - c# WebBrowser DocumentText works once but not in a loop? PropertyChangedEventHandler都为空,但是绑定只能工作一次,为什么? - PropertyChangedEventHandler is both times null, but the binding works only once, why? c#为什么通过线程调用WebClient时,大多数时间都会超时? - c# Why the WebClient times out most of the timeswhen it is invoked through a thread? webclient烦恼或什么? - webclient bugging out or what? WebClient DownloadStringCompleted仅触发一次 - WebClient DownloadStringCompleted only fires once 尝试在随机生成 1 或循环 10 次后结束 while 循环 - Trying to end a while loop once a 1 is randomly generated or it loops 10 times 将控件添加到面板中的工作在前几次中起作用,然后逐渐消失 - Adding controls to the Panel works the first couple of times, then fritzes out Web服务方法调用超时,元数据请求正常运行 - Web service method call times out, metadata request works fine C#HttpWebRequest响应超时,在浏览器中工作 - C# HttpWebRequest response times out eventough works in browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM