这事一个开始加载url的 方法 ,在 加载 完成时发生 webClient_OpenReadCompleted方法:
private void DoWebClient()
{
WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri("http://www.cnblogs.com/linzheng"));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}
这个方法是url请求完成时调用的,这里读取返回流显示在textBox1:
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (StreamReader reader = new StreamReader(e.Result))
{
string contents = reader.ReadToEnd();
int begin = contents.ToString().IndexOf("<title>");
int end = contents.ToString().IndexOf("</title>");
string note = contents.Substring(contents.ToString().IndexOf("摘要"), 300);
webClientTextBlock.Text = contents.ToString().Substring(begin + 7, end - begin -7);
textBox1.Text = note;
}
}