Solved

VB.net Webclient - trying not to cache webpage

Posted on 2015-02-12
4
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

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 36

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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