Link to home
Start Free TrialLog in
Avatar of keith1001
keith1001Flag for United States of America

asked on

VB.net Webclient - trying not to cache webpage

The main thing I am trying to do is get page load time of a website.  But it seems to work the first time I put a new website domain in, but run it again it appears to be a lot faster (cache). I dont want that, I want it make sure it does not load cache version.  Any ideas on what I did wrong below?  I'm also open to a better way to get total page load time.

Dim sw = New Stopwatch()
sw.Start()
Dim policy As New RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
Dim sourceString As String = New System.Net.WebClient().DownloadString("http://www.google.com")

 sw.Stop()
 Dim ts As TimeSpan = sw.Elapsed
        txtWebSource.Text = sourceString
        ' Format and display the TimeSpan value. 
        Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 1)
        lblPingTime.Text = elapsedTime

Open in new window

Avatar of Kimputer
Kimputer

You didn't actually use the policy. Make sure WebClient.CachePolicy is used in your code.

Please note that the numbers AFTER the first run are pretty accurate, even if you DIDN'T use the policy (I only mentioned you didn't use it so in your further .net programming, setting a variable and not using it later is senseless). Cache isn't used (you can capture your network traffic, you will see it is indeed reloaded from the server every time).
Since you used a quick website, you would think it's not accurate. If you tested on a foreign slow website, you probably wouldn't notice it.
Why the first run is always a bit higher has probably something to do with the .Net framework.
Avatar of keith1001

ASKER

I have tested it on bigger sites, and the 1st run is always longer by a couple seconds.

So what do I need to add the set the policy to NoCache or Reload?
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for long time for accepting, fighting some health issues unfortunately.