循环下载的 C# Webclient 问题? [英] C# Webclient problem with looping download?

查看:30
本文介绍了循环下载的 C# Webclient 问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对 Webclient 的实现,据说这个下载应该是连续的,但出于某种原因,调试甚至没有帮助,我在第一次运行中获得了 1 次成功,然后其余的都失败了.有谁知道为什么?

this is my implementation of Webclient, supposedly, this download should be continuous, but for some reason, which debug don't even help, i got 1 success in the first run, then the rest are failed. Does anyone know why ?

        for (int i = 1; i <= Count; i++)
        {
            using (WebClient wc = new WebClient())
            {

                wc.Headers["Accept-Encoding"] = "gzip";
                wc.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                byte[] arr = wc.DownloadData(url);

                if (arr.Length > 0)
                    Console.WriteLine(i.ToString() + ": SUCCESS");
                else
                    Console.WriteLine(i.ToString() + ": FAILED");
            }

        }

推荐答案

当我弄乱了这段代码时,它起作用了,哈哈!我不知道该说什么了...

when i messed up with this code, it work, LOL! I dont' know what to say anymore...

                using (WebClient client = new WebClient())
                {
                    //manipulate request headers (optional)
                    client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                    //execute request and read response as string to console
                    using (StreamReader reader = new StreamReader(client.OpenRead(url)))
                    {
                        string s = reader.ReadToEnd();
                        //Console.WriteLine(s);
                        Console.WriteLine("Vote " + i.ToString() + ": SUCCESS");
                        i++;
                    }
                }

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

                // *** Set properties
                loHttp.Timeout = 10000;     // 10 secs
                loHttp.UserAgent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                loHttp.Headers["Accept-Encoding"] = "gzip";
                // *** Retrieve request info headers
                HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

                Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page

                StreamReader loResponseStream =
                   new StreamReader(loWebResponse.GetResponseStream(), enc);

                string lcHtml = loResponseStream.ReadToEnd();

                loWebResponse.Close();
                loResponseStream.Close();

                if (lcHtml.Length > 0)
                {
                    Console.WriteLine("Vote " + i.ToString() + ": SUCCESS");
                    i++;
                }
                else
                    Console.WriteLine("Vote " + i.ToString() + ": FAILED");
            }

标记为社区维基,以便如果有人知道原因,请编辑... :(

Marked as community wiki, so that if anyone know why, please edit... :(

这篇关于循环下载的 C# Webclient 问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆