当winform的webbrowser浏览一组url地址时返回

时间:2014-08-02 16:31:57

标签: c# winforms webbrowser-control

我需要浏览一组网址(是的另一个网络刮刀......)。 我想用任务。但是我在浏览器完成后返回时遇到问题。

为了确保网站已满载,我必须跳转到document_completed,然后从那里用另一个网址调用Navigate方法。

这样的事情:

private WebBrowser browser;
private List<string> urlsToVisit;
int urlCounter = 0;

public PageBrowser(List<string> urls) //constructor
{
    urlCounter = 0;
    urlsToVisit = urls;
    browser = new WebBrowser(); //one instance of browser for all urls
    browser.ScriptErrorsSuppressed = true;
    browser.DocumentCompleted += browser_DocumentCompleted;
}

//this I want to call from somewhere else and return true AFTER it opens all sites
public bool Run()
{
    VisitPages();
    return true;
}

private void VisitPages()
{         
    if (urlCounter < urlsToVisit.Count)
    {
        browser.Navigate(urlsToVisit[urlCounter]);               
        urlCounter++;
    }
    else
    {
        browser.Dispose();
    }
}

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath) return;
    System.Threading.Thread.Sleep(3000); //random interval between requests
    VisitPages();
}

我很确定,解决方案非常简单,但我只是看不到它。

谢谢

彼得

1 个答案:

答案 0 :(得分:0)

我建议您使用WebClient代替WebBrowser UI组件,因为您似乎不需要渲染任何内容。您似乎只想将请求发送到您的网址列表,如果一切顺利,则返回true,如果出现任何类型的问题,则返回false。

private async Task<bool> VisitUrls()
{
    var urls = new List<string>
    {
        "http://stackoverflow.com", 
        "http://serverfault.com/", 
        "http://superuser.com/"
    };
    var success = await BrowseUrls(urls);
    return success;
}

private async Task<bool> BrowseUrls(IEnumerable<string> urls)
{
    var timeoutWebClient = new WebClient();
    foreach (var url in urls)
    {
        try
        {
            var data = await timeoutWebClient.DownloadDataTaskAsync(url);
            if (data.Length == 0) return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    //Everything went well, returning true
    return true;
}