HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseurl + url);
req.Timeout = 1000 * 10;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream str = response.GetResponseStream();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(str);
response.Close();
string imgurl = doc.DocumentNode.SelectSingleNode("//div[@class='one-page']/a/img[@class='manga-page']").Attributes["src"].Value;
req = (HttpWebRequest)HttpWebRequest.Create(imgurl);
req.Timeout = 1000 * 10;
response = (HttpWebResponse)req.GetResponse();
str = response.GetResponseStream();
Image img = Image.FromStream(str);
response.Close();
return img;

我循环运行此代码(使用多个线程)以下载约4000张图像,并且它在前几百个图像中表现出色,但随后(在每次尝试的不同时间点)它都突然停止工作,并且每次调用“req.GetResponse()”导致TimeoutException。我不知道为什么会这样,也不知道可能是什么错误或如何处理。任何帮助将不胜感激。

我用于运行此函数的代码(称为GetPage(int)并称为c.GetPage(t))如下:
for (int j = 0; j < 2; j++)
{
    BackgroundWorker bw = new BackgroundWorker();
    num[bw] = j;
    bgs.Add(bw);
    bw.DoWork += (object sender, DoWorkEventArgs doargs) =>
    {
        int t = -1;
        lock (lockObjForQueueOperations)
        {
            if (images.Count != 0)
                t = images.Dequeue();
        }
        if(t < 0)
        {
            doargs.Result = false;
            return;
        }
        currently[sender] = t;
        Image img;
        try { img = c.GetPage(t); }
        catch (Exception e)
        {
            lock (lockObjForQueueOperations)
            {
                images.Enqueue(t);
            }
            lock (Console.Title)
            {
                if (num[sender] == 0) Console.ForegroundColor = ConsoleColor.Cyan;
                else if (num[sender] == 1) Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**ERR: Error fetshing page {0}, errormsg: {1}", t, e.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
            doargs.Result = true;
            Thread.Sleep(1000*2);
            return;
        }
        lock (Console.Title)
        {
            if (num[sender] == 0) Console.ForegroundColor = ConsoleColor.Cyan;
            else if (num[sender] == 1) Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\t\tLoaded page {0} of {1}.", t + 1, c.PagesCount);
            Console.ForegroundColor = ConsoleColor.White;
        }
        string imgpath = Path.Combine(ndir, "Page " + (t + 1) + ".png");
        img.Save(imgpath, System.Drawing.Imaging.ImageFormat.Png);
        img.Dispose();
        doargs.Result = true;
    };
    bw.RunWorkerCompleted += (object sender, RunWorkerCompletedEventArgs runargs) =>
    {
        if ((bool)runargs.Result) bw.RunWorkerAsync();
        else
        {
            finnishedworkers++;
            if (finnishedworkers == 2) restetter.Set();
            bw.Dispose();
        }
    };
    bw.RunWorkerAsync();
}

最佳答案

HttpWebRequest中的Timeout属性以毫秒为单位。当前将其设置为10,000仅仅是10秒,而这可能还不足以解决带宽,要提取的数据大小以及正在运行的代码的复杂性。我说先尝试增加它。

关于c# - Webrequest突然停止工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2292675/

10-10 23:45