<% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %> <% ' This sample examines the caching side of things. For more ' details on the HTTP request aspect of the code, see our two ' previous HTTP samples: ' http://www.asp101.com/samples/winhttp5.asp (WinHttp version) ' http://www.asp101.com/samples/http.asp (older MSXML version) Dim strCached ' string to hold text telling user if page was cached ' Check to see if the expiration date of the cached data has passed. If Application("http_cache_sample_expires") < Now() Then Dim objWinHttp ' HTTP request object Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1") objWinHttp.Open "GET", "http://www.asp101.com/samples/httpsamp.asp" objWinHttp.Send ' If we get a valid response then we save the data to an ' application variable and set another to indicate how long ' the data should be kept. If objWinHttp.Status = "200" Then Application.Lock ' Save the response to an application level variable Application("http_cache_sample_content") = objWinHttp.ResponseText ' Set the expiration time. As an example I'm adding 1 minute to ' the current time, but you can do whatever works best for you. Application("http_cache_sample_expires") = DateAdd("n", 1, Now()) Application.UnLock End If ' Trash our HTTP object now that I'm finished with it. Set objWinHttp = Nothing strCached = "Not Cached" Else strCached = "Cached" End If %>

Retreived HTML (<%= strCached %>):

<%= Server.HtmlEncode(Application("http_cache_sample_content")) %>

The cached data will expire at: <%= Server.HtmlEncode(Application("http_cache_sample_expires")) %>

The content in the box is retrieved via HTTP and is then cached for up to one minute. As you refresh the page, pay attention to the timestamp in the returned HTML. For comparison, it is currently: <%= Now() %>