In multi thread download program only one backgroundthread is working and next one throwing the operation time out exception

StackOverflow https://stackoverflow.com/questions/20267745

I am trying to download a file with multi backgroundworkers.

I have the below code in Download method,

     private static CookieContainer c = new CookieContainer();
    public static void Download(string url, string from, string to, string method, string fileName)
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = method;
        req.AllowAutoRedirect = true;
        req.Timeout = 12000;
        req.CookieContainer = c;
        req.AddRange(Convert.ToInt32(from), Convert.ToInt32(to));
        using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
        {
            Console.Out.WriteLine((int)res.StatusCode + " ");
            using (Stream output = res.GetResponseStream())
            {
                  using (Stream file = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int len;
                     while ((len = output.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        file.Write(buffer, 0, len);
                        //Console.Out.WriteLine("{0}", fileName);
                    }
                }
            }
        }
        Console.Out.WriteLine("Complated!");
     }

each BackgroundWorker should call this, but only the first BackGroundWorker is working and the next one throwing the "The operation timeout" exception

currently Second Worker is calling after the complete execution of the first worker only, but i want to make this multi threaded app

Can any one help me please....

please let me know if you need any further information.

Thanks, Srinivas

---------------------Update----------------------------------------

Now added a shared cookie and it is allowing only 2 threads one after one, means after the first download then it is trying to download the next one. How can i make it symultanious download?

Can any one help me please....

Thanks Srini

有帮助吗?

解决方案

currently Second Worker is calling after the complete execution of the first worker only

No, that's most certainly the wrong diagnostic. Because if that was the case then the second worker would not call req.GetResponse() until the first worker was done. So could not timeout immediately.

Something is throttling the number of concurrent http requests you can start at the same time. The server itself is certainly high on the list of candidates, particularly the kind of server that supplies the kind of data where this kind of technique makes sense. They certainly protect themselves against this. That another program can bypass this doesn't otherwise prove anything, it could do something nasty to defeat the server's detection algorithm like spoofing the IP address (I'm reaching). Lots of other candidates, like a proxy server or something silly in the anti-malware.

Do verify the server's terms of usage statement, pretty unlikely that the server owner is happy about you doing this.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top