C# 从网站获取html
我可以通过以下方式从网站获取html代码:C# 从网站获取html,c#,silverlight,windows-phone-7,httpwebrequest,C#,Silverlight,Windows Phone 7,Httpwebrequest,我可以通过以下方式从网站获取html代码: public void Test() { WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new Uri("http://
public void Test()
{
WebClient client = new WebClient();
client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://testUrl.xml"));
}
void client_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
string html = e.Result;
//Now do something with the string...
}
但我需要每30秒更新一次html,所以我写道:
public void TestMain()
{
DispatcherTimer Timer = new DispatcherTimer()
{
Interval = TimeSpan.FromSeconds(30)
};
Timer.Tick += (s, t) =>
{
Test();
};
Timer.Start();
}
我更改了xml,但得到了相同的html,怎么了?网络客户端中包含了缓存。如果您两次请求相同的URI,第二次它将直接从缓存中获取整个内容 无法在
WebClient
上禁用缓存,因此您有两种解决方法:
- 使用
而不是HttpWebRequest
WebClient
- 向URI添加随机参数:
client.DownloadStringAsync(new Uri("http://testUrl.xml?nocache=" + Guid.NewGuid()));