ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @ Microsoft.com
What is Adovbs.inc and Why Do I Need It?

QUICK TIP:
Sending an Email to Multiple Recipients
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


How to Access the Network using ASP.NET
(aka. HTTP using ASP.NET)

by Eng. Samer Nazzal

As a web developer you need to grab the HTML content of the Web page. The WebResponse and WebRequest Abstract classes, that provided by the .NET Framework, can be used for accessing information over the Network (Internet). Instances for the WebRequest and WebResponse classes must be created via the WebRequest class and the GetResponse() method of the WebRequest class.

The System.Net namespace, which contains the WebResponse and WebRequest classes that we'll be using. The System.IO namespace is also used because we will be using the Stream and StreamReader classes to access the response HTML.

To grab the HTML at the URL specified by the user, we need to make an HTTP request to the URL using the WebRequest class.

1: 'Create a WebRequest
2: Dim wrRequest As WebRequest
3: wrRequest = WebRequest.Create(txtURL.Text)

On line 3, a variable of type WebRequest is created. On line 3, this variable is instantiated, assigned to the WebRequest instance returned by the Create method of the WebRequest.

Then we need to grab the response stream from the Web request we just made; the WebResponse class handles this.

4: 'Get the Response from the Request
5: Dim wrResponse As WebResponse = wrRequest.GetResponse()

On line 5 a WebResponse variable is created and assigned to the WebResponse object returned by the GetResponse() method of the WebRequest class.

The WebRequest and WebResponse classes both have a Headers property, which represents an instance of the WebHeaders class. Through this property, a developer can programmatically send request headers and iterate through the response headers.

 6: 'Display the Request headers
 7: LBLHTML.Text = "<b>Request Header</b><br>"
 8: Dim strHeader As String
 9: For Each strHeader In wrRequest.Headers
10:    LBLHTML.Text += strHeader + "--" + _ 
11:    wrRequest.Headers(strHeader) + "<br>"
12: Next
13: 'Display the Response headers
14: LBLHTML.Text += "<p><b>Response Header</b><br>"
15: For Each strHeader In wrResponse.Headers
16:    LBLHTML.Text += strHeader + "--" + _ 
17:    wrResponse.Headers(strHeader) + "<br>"
18: Next

On lines 6 through 12, the request headers are displayed using a foreach loop. On lines 13 through 18, the response headers are displayed in a similar fashion.

Next, we need to pick out the content from the WebResponse variable wrResponse . The WebResponse class contains a GetResponseStream() method, which returns a Stream object that contains the HTML returned by the URL specified by the WebRequest instance.

19: 'Read the Response into a stream and output the stream
20: Dim objStream As Stream = wrResponse.GetResponseStream()
21: Dim objStreamReader As StreamReader = New StreamReader(objStream)
22: LBLHTML.Text += objStreamReader.ReadToEnd()

On line 20, a Stream instance is created, objStream, and is assigned to the Stream instance returned by the GetResponseStream() method. To read the contents of this Stream, we must use the StreamReader class. On line 21, a StreamReader class is instantiated, passing the objStream variable to the constructor. Finally, the complete contents of the Stream are displayed on line 22 via the ReadToEnd() method of the StreamReader object.

The Output will be:

Sample Page Output Screen Capture

The HTML Code:

<%@ Page Language="vb" AutoEventWireup="false"
   Codebehind="SimpleIE.aspx.vb" Inherits="Articles.SimpleIE"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML dir="ltr">
  <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
  </HEAD>
<body MS_POSITIONING="GridLayout">
 <form id="Form1" method="post" runat="server">
<TABLE id="Table1" dir="ltr" style="Z-INDEX: 105; LEFT: 2px;
    WIDTH: 731px; POSITION: absolute; TOP: 2px; HEIGHT: 15px"
    cellSpacing="1" cellPadding="1" width="731" align="left"
    border="1">
    <TR>
      <TD style="HEIGHT: 3px" bgColor="ivory">
        <asp:Label id="LBLAddress"
          runat="server">Address</asp:Label></TD>
      <TD style="WIDTH: 526px; HEIGHT: 3px" bgColor="ivory">
        <asp:TextBox id="txtURL" runat="server"
          Width="525px"></asp:TextBox></TD>
      <TD style="HEIGHT: 3px" bgColor="ivory">
        <asp:Button id="BTNSubmit" runat="server" Width="131px"
          Text="Go .."  Height="24px"></asp:Button></TD>
    </TR>
    <TR>
      <TD style="HEIGHT: 3px" colSpan="3">
        <asp:Label id="LBLHTML" runat="server"
          Width="721px" Height="9px"
          BackColor="Transparent">Label</asp:Label></TD>
    </TR>
  </TABLE>
 </form>
</body>
</HTML> 

The Code-Behind "SimpleIE.aspx.vb":

Imports System.IO
Imports System.Net
Public Class SimpleIE
  Inherits System.Web.UI.Page
  Protected WithEvents txtURL As System.Web.UI.WebControls.TextBox
  Protected WithEvents LBLAddress As System.Web.UI.WebControls.Label
  Protected WithEvents BTNSubmit As System.Web.UI.WebControls.Button
  Protected WithEvents LBLHTML As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()>
  Private Sub InitializeComponent()
  End Sub
  Private Sub Page_Init(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
  End Sub
#End Region
Private Sub BTNSubmit_Click(ByVal sender As System.Object,
  ByVal e As System.EventArgs) Handles BTNSubmit.Click
    'Create a WebRequest
    Dim wrRequest As WebRequest
    wrRequest = WebRequest.Create(txtURL.Text)
    'Get the Response from the Request
    Dim wrResponse As WebResponse = wrRequest.GetResponse()
    'Display the Request headers
    LBLHTML.Text = "<b>Request Header</b><br>"
    Dim strHeader As String
    For Each strHeader In wrRequest.Headers
      LBLHTML.Text += strHeader + "--" _
        + wrRequest.Headers(strHeader) + "<br>"
    Next
    'Display the Response headers
    LBLHTML.Text += "<p><b>Response Header</b><br>"
    For Each strHeader In wrResponse.Headers
      LBLHTML.Text += strHeader + "--" _
        + wrResponse.Headers(strHeader) +  "<br>"
    Next
    'Read the Response into a stream and output the stream
    Dim objStream As Stream = wrResponse.GetResponseStream()
    Dim objStreamReader As StreamReader = New StreamReader(objStream)
    LBLHTML.Text += objStreamReader.ReadToEnd()
  End Sub
End Class

Related Info


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers