Solved

VB.net Webclient - trying not to cache webpage

Posted on 2015-02-12
4
210 Views
Last Modified: 2015-03-06
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

0
Comment
Question by:keith1001
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 40605340
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.
0
 

Author Comment

by:keith1001
ID: 40607298
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?
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40607559
Read my message again. I said even if you didn't use the policy, the results will largely STILL be the same (first run longer). You should discard the first result, it's a .NET thing. All subsequent timings are correct.
So Policy to NoCache, Reload or NOT SETTING it, it doesn't matter. Your application WILL get the files from the server, NOT from disk or memory.
0
 

Author Closing Comment

by:keith1001
ID: 40650492
Sorry for long time for accepting, fighting some health issues unfortunately.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now