http.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<SCRIPT Language="VB" Option="Explicit" runat="server">
Dim strHTMLLong As String
Dim strHTMLShort As String
Sub Page_Load(Src as object, E as EventArgs)
strHTMLLong = GetWebPageAsStringLong("http://www.asp101.com/samples/httpsamp.asp")
strHTMLShort = GetWebPageAsStringShort("http://www.asp101.com/samples/httpsamp.asp")
End Sub
' Before I start I should say that there are a number
' of ways to do this (like anything), but this was the
' one that made the most sense to me when I was writing
' it. If you think you've got a better way let me know
' via email -> john@asp101.com.
' In this version I Dim all my variables first in
' a more traditional VB style.
Function GetWebPageAsStringLong(strURI As String) As String
Dim objURI As URI
Dim objWebRequest As WebRequest
Dim objWebResponse As WebResponse
Dim objStream As Stream
Dim objStreamReader As StreamReader
Dim strHTML As String
objURI = New URI(strURI)
objWebRequest = WebRequest.Create(objURI)
objWebResponse = objWebRequest.GetResponse()
objStream = objWebResponse.GetResponseStream()
objStreamReader = New StreamReader(objStream)
strHTML = objStreamReader.ReadToEnd
GetWebPageAsStringLong = strHTML
End Function
' In this version I Dim all my variables as I need
' them and assign values to them on the same line.
' It results in a much shorter set of code.
' Even if it is wider ;)
Function GetWebPageAsStringShort(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
GetWebPageAsStringShort = strHTML
End Function
</SCRIPT>
<html>
<head>
<title>ASP.NET HTTP Request Sample from ASP 101</title>
</head>
<body>
<h2>strHTMLLong:</h2>
<pre>
<%= Server.HTMLEncode(strHTMLLong) %>
</pre>
<h2>strHTMLShort:</h2>
<pre>
<%= Server.HTMLEncode(strHTMLShort) %>
</pre>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/http_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>