HttpWebRequest Demo

The following demo illustrates how to request HTML content from a remote Web server using the HttpWebRequest class. In this live demo, we simply grab the HTML from www.yahoo.com and display it in a textbox.


The following textbox contains the HTML returned by a request to http://www.yahoo.com


Source Code
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

<script runat="server" language="VB">

Sub Page_Load(sender as Object, e as EventArgs)
	'Create the HttpWebRequest object
	Dim req as HttpWebRequest = WebRequest.Create("http://www.yahoo.com/")

	Try
	  'Get the data as an HttpWebResponse object
	  Dim resp as HttpWebResponse = req.GetResponse()

	  'Convert the data into a string (assumes that you are requesting text)
	  Dim sr as New StreamReader(resp.GetResponseStream())
	  Dim results as String = sr.ReadToEnd()
	  sr.Close()
	  
	  html.Text = results
	Catch wex as WebException
	  Response.Write("<font color=red>SOMETHING WENT AWRY!<br />Status: " & wex.Status & "
Message: " & wex.Message & "</font>") End Try End Sub </script> <form runat=server> The following textbox contains the HTML returned by a request to http://www.yahoo.com <p> <asp:TextBox runat=server id="html" TextMode="MultiLine" Columns="60" Rows="15" /> </form>

[Return to the article...]