简体   繁体   English

您能举一个WebClient下载网站源代码并异步过滤某些内容以免冻结GUI的示例吗?

[英]Could you give me an example of a WebClient downloading a website source code and filtering something asynchronously as to not freeze the GUI?

I've read some of the articles on MSDN but they're not quite what I need right now. 我已经阅读了MSDN上的一些文章,但它们并不是我现在所需要的。

Could you show me a simple application that downloads a website asyncchronously (WebClient.DownloadDataAsync) and then filters the TITLE tag of the site? 您能告诉我一个简单的应用程序,该应用程序异步下载网站(WebClient.DownloadDataAsync),然后过滤该网站的TITLE标签吗?

I can't do this for the life of me, and I'm just trying to see a clear cut model. 我一生无法做到这一点,我只是想看看一个清晰的模型。

You may find it easier to use DownloadStringAsync. 您可能会发现使用DownloadStringAsync更容易。 That way, you can parse the HTML as a string rather than having to deal with encoding yourself. 这样,您可以将HTML解析为字符串,而不必自己编码。

As far as how to parse the title, you may find that a bit more difficult to do, since .NET doesn't have a built-in HTML parser. 至于标题的解析方式,您可能会发现很难做,因为.NET没有内置的HTML解析器。 You could try some RegEx or use XMLReader, but those can be problematic if you have malformed or tricky content. 您可以尝试一些RegEx或使用XMLReader,但是如果您的内容格式不正确或比较棘手,则可能会出现问题。

var client = new WebClient();
client.DownloadStringCompleted += (s, args) => {
    if (args.Error == null && !args.Cancelled) {
        var regex = new Regex("<title>(?<title>.*?)</title>");
        var match = regex.Match(args.Result);
        if (match.Success)
        {
            var myTitle = match.Groups["title"].Value;
            // ... 
        }
    }
};
client.DownloadStringAsync(url);

Here is a complete solution that uses asynchronous delegates . 这是使用异步委托的完整解决方案。 Notice that I am using Control.Invoke to ensure that any UI interactions are performed on the main UI thread, in this case displaying a message box. 请注意,我正在使用Control.Invoke来确保在主UI线程上执行任何UI交互,在这种情况下,显示消息框。

var button = new Button {Text = "Run"};
button.Click +=
    (sender, e) =>
        {
            var fetchTitle = new Func<string, string>(
                address =>
                    {
                        var html = new WebClient().DownloadString(address);
                        var match = Regex.Match(html, "<title>(.*)</title>");
                        return match.Groups[1].Value;
                    });
            var displayTitle = new Action<string>(
                title => MessageBox.Show(title));
            fetchTitle.BeginInvoke(
                "http://stackoverflow.com",
                result =>
                    {
                        var title = fetchTitle.EndInvoke(result);
                        if (button.InvokeRequired)
                        {
                            button.Invoke(displayTitle, title);
                        }
                        else
                        {
                            displayTitle(title);
                        }
                    },
                null);
        };
new Form {Controls = {button}}.ShowDialog();

暂无
暂无

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

相关问题 WebClient-POST后如何获取源 - WebClient - How to get source after you POST'ed something 你能给我一个在.NET中导致内存碎片的例子吗? - Can you give me an example that causes memory fragmentation in .NET WebClient.DownloadString()使我的应用程序在“下载”源代码期间冻结 - WebClient.DownloadString() makes my application freeze during the “download” of the source code WebClient异步下载文件时检测连接丢失(在C#中) - detecting connection drops while WebClient is downloading a file asynchronously (in C#) 使用WebClient异步下载文件时,DownloadFileCompleted事件不起作用 - DownloadFileCompleted event not working while asynchronously downloading files using webclient .NET:我是否需要在异步下载时保留对WebClient的引用? - .NET: Do I need to keep a reference to WebClient while downloading asynchronously? C#WebClient()下载网站的特定部分 - C# WebClient() Downloading a specific part of a website C#:谁能给我一个很好的例子,说明如何在运行时锚定控件? - C#: Could anyone give me good example on how anchoring controls at runtime is done? 当我使用“显示器”时,因为不能用“锁定”来完成,您能举一个简单的例子吗? - Can you give me a simple example when I am to use “monitor” because it cannot be done with “lock”? C#使用C#WebClient或HttpWebRequest将网站下载到字符串中 - C# Downloading website into string using C# WebClient or HttpWebRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM