[英]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.