http_cache.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<SCRIPT Language="VB" Option="Explicit" runat="server">
Dim strRetrievedHTML As String
Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
If IsNothing(Cache("http_cache_sample")) Then
strRetrievedHTML = GetWebPageAsString("http://aspnet.asp101.com/samples/httpsamp.aspx")
Cache.Insert("http_cache_sample", strRetrievedHTML, _
Nothing, DateTime.Now.AddMinutes(1), TimeSpan.Zero)
litCached.Text = "(Not Cached)"
Else
strRetrievedHTML = Cache("http_cache_sample")
litCached.Text = "(Cached)"
End If
'Cache.Remove("http_cache_sample")
End Sub
Function GetWebPageAsString(ByVal strURI As String) As String
Dim objURI As Uri = New Uri(strURI)
Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
Dim objStream As Stream = objWebResponse.GetResponseStream()
Dim objStreamReader As StreamReader = New StreamReader(objStream)
Dim strHTML As String = objStreamReader.ReadToEnd
GetWebPageAsString = strHTML
End Function
</SCRIPT>
<html>
<head>
<title>ASP.NET HTTP Request (Cached) Sample from ASP 101</title>
</head>
<body>
<h2>Retreived HTML <asp:Literal id="litCached" runat="server" />:</h2>
<table border="1" cellpadding="0" cellspacing="0">
<tr><td>
<pre>
<%=Server.HtmlEncode(strRetrievedHTML)%>
</pre>
</td></tr>
</table>
<p>
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().ToLongTimeString %>
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/http_cache_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>