revolutionkpi
revolutionkpi

Reputation: 2682

Get html from website

I can get an html code from web site this way:

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...
}

But I need to get updated html each 30 seconds, so I wrote:

public void TestMain()
{

    DispatcherTimer Timer = new DispatcherTimer()
    {
        Interval = TimeSpan.FromSeconds(30)
    };
    Timer.Tick += (s, t) =>
    {
        Test();
    };
    Timer.Start();
}

I changed the xml but I get the same html, what is wrong?

Upvotes: 1

Views: 1427

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39027

There's a cache included in the WebClient. If you request the same URI twice, the second time it will fetch the whole content directly from the cache.

There's no way to disable cache on WebClient, so you have two workarounds:

  • Use HttpWebRequest instead of WebClient
  • Add a random parameter to the URI:

    client.DownloadStringAsync(new Uri("http://testUrl.xml?nocache=" + Guid.NewGuid()));
    

Upvotes: 3

Related Questions