Web Request Class


This is a simple application that the gets the source of a webpage via the WebRequest Object. 



The WebRequest class is defined in System.Net namespace. WebRequest is an abstract class. It is the base class for accessing data from the internet in the .Net Framework.

This tutorial would show you how to access data on the internet using the WebRequest Class.

Adding namespace Reference

Since the WebRequest class is defined in the System.Net namespace, so the first thing to do is to Add the System.Net reference to the project.

using
System.Net;

Creating the WebRequest Object

The WebRequest Object should not be created directly. It is an abstract object. To create the WebRequest you should use the WebRequestFactory object. The WebRequestFactory class is a static class that returns an instance of an object derived from WebRequest.

You use the create method of the WebRequestFactory class. Here is an example:

// You could pass the create method a string or a URI
WebRequest WReq = WebRequestFactory.Create(http://www.yahoo.com);

OR

URI uriWebSite = new URI("http://www.yahoo.com");
WebRequest WReq = WebRequestFactory.Create(uriWebSite);

To get the response from the server you call the GetResponse() Method on the WebRequest object.
WebResponse wResp = WReq.GetResponse();

Getting the Stream of Data from the WebResponse Object

Getting the Stream of data you use the GetResponseStream() Method on the WebResponse Object and you specify how you want to encode the data you are getting back.

StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII);

That's it folks.

Source Code

namespace HTMLSourceViewer
{
using System;
using System.Net;
using System.Text;
using System.IO;
using System.WinForms;
public class GetWebPageSource
{
public GetWebPageSource() { }
public GetWebPageSource(string webAddress)
{
this. webAddress = webAddress;
}
public string GetSource()
{
StringBuilder strSource =
new StringBuilder("");
try
{
WebRequest WReq = WebRequestFactory.Create(
this.webAddress);
WebResponse WResp = WReq.GetResponse();
// get the stream of data
StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII);
string strTemp = "";
while ((strTemp = sr.ReadLine()) != null)
{
strSource.Append(strTemp + "\r\n");
}
sr.Close();
}
catch (WebException WebExcp)
{
MessageBox.Show(WebExcp.Message, "Error", MessageBox.IconError);
}
return strSource.ToString();
}
// Accessor method
public void setWebAddress(string strNewAddress)
{
this.webAddress = strNewAddress;
}
// private variables
private string webAddress;
}
}
namespace HTMLSourceViewer
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Remoting;
public class Form1 : System.WinForms.Form
{
private System.ComponentModel.Container components;
private System.WinForms.Label label2;
private System.WinForms.TextBox textBox2;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.TextBox textBox1;
private System.WinForms.Label label1;
private const string HTTP = "http://"; // scheme identifier
private string strSource = null;
public Form1()
{
InitializeComponent();
this.Height = 100;
}
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.button1 = new System.WinForms.Button ();
this.label1 = new System.WinForms.Label ();
this.label2 = new System.WinForms.Label ();
this.textBox1 = new System.WinForms.TextBox ();
this.button2 = new System.WinForms.Button ();
this.textBox2 = new System.WinForms.TextBox ();
//@this.TrayHeight = 0;
//@this.TrayLargeIcon = false;
//@this.DrawGrid = false;
//@this.TrayAutoArrange = true;
button1.Location = new System.Drawing.Point (544, 24);
button1.ForeColor = System.Drawing.Color.Maroon;
button1.BackColor = System.Drawing.Color.Khaki;
button1.Size =
new System.Drawing.Size (88, 24);
button1.TabIndex = 2;
button1.Text = "Get Source";
button1.Click +=
new System.EventHandler (this.button1_Click);
label1.Location =
new System.Drawing.Point (16, 24);
label1.Text = "Web Address";
label1.Size =
new System.Drawing.Size (89, 18);
label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label1.AutoSize =
true;
label1.Font =
new System.Drawing.Font ("Microsoft Sans Serif", 10);
label1.TabIndex = 0;
label1.BackColor = System.Drawing.Color.Khaki;
label2.Location =
new System.Drawing.Point (16, 64);
label2.Text = "WebPage Source";
label2.Size =
new System.Drawing.Size (114, 18);
label2.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
label2.AutoSize =
true;
label2.TabIndex = 5;
label2.Anchor = System.WinForms.AnchorStyles.All;
label2.BackColor = System.Drawing.Color.Khaki;
label2.Visible =
false;
textBox1.Location =
new System.Drawing.Point (112, 24);
textBox1.Text = "http://";
textBox1.ForeColor = System.Drawing.Color.Maroon;
textBox1.Font =
new System.Drawing.Font ("Microsoft Sans Serif", 10);
textBox1.TabIndex = 1;
textBox1.Size =
new System.Drawing.Size (424, 23);
textBox1.BackColor = System.Drawing.Color.AntiqueWhite;
button2.Location =
new System.Drawing.Point (640, 24);
button2.ForeColor = System.Drawing.Color.Maroon;
button2.BackColor = System.Drawing.Color.Khaki;
button2.Size =
new System.Drawing.Size (56, 24);
button2.TabIndex = 3;
button2.Text = "Close";
button2.Click +=
new System.EventHandler (this.button2_Click);
textBox2.Location =
new System.Drawing.Point (16, 96);
textBox2.Multiline =
true;
textBox2.BorderStyle = System.WinForms.
BorderStyle.FixedSingle;
textBox2.ScrollBars = System.WinForms.ScrollBars.Both;
textBox2.ForeColor = System.Drawing.Color.Maroon;
textBox2.TabIndex = 4;
textBox2.Size =
new System.Drawing.Size (680, 480);
textBox2.BackColor = System.Drawing.Color.PapayaWhip;
textBox2.Visible =
false;
this.Text = "Kareem's HTMLSource Viewer";
this.AutoScaleBaseSize = new System.Drawing.Size (6, 16);
this.KeyPreview = true;
this.AutoScroll = true;
this.Font = new System.Drawing.Font ("Microsoft Sans Serif", 10);
this.BackColor = System.Drawing.Color.Goldenrod;
this.ClientSize = new System.Drawing.Size (704, 581);
this.Controls.Add (this.label2);
this.Controls.Add (this.textBox2);
this.Controls.Add (this.button2);
this.Controls.Add (this.button1);
this.Controls.Add (this.textBox1);
this.Controls.Add (this.label1);
}
protected void button2_Click (object sender, System.EventArgs e)
{
Application.Exit();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
char chr = e.KeyChar;
//13 = Enter key
if (chr == 13)
{
GetPageSource();
}
}
protected void button1_Click (object sender, System.EventArgs e)
{
GetPageSource();
}
public void GetPageSource()
{
string strAddress = textBox1.Text.Trim();
strAddress = strAddress.ToLower();
if ( strAddress.Length <= HTTP.Length)
{
MessageBox.Show("Enter a web address", "Web Address Field", MessageBox.IconInformation);
textBox1.Text = HTTP;
}
else if (!strAddress.StartsWith(HTTP))
{
MessageBox.Show("You have Entered the wrong Protocol.", "Wrong Scheme Identifier", MessageBox.IconInformation);
textBox1.Text = HTTP;
}
else
{
// create the GetWebPageSource object
GetWebPageSource objGS = HTMLSourceViewer.GetWebPageSource();
objGS.setWebAddress(strAddress);
strSource = objGS.GetSource();
if (strSource.Length > 1)
{
showSource();
}
}
}
public void showSource()
{
Form1.ActiveForm.Height = 608;
textBox2.Text = strSource;
label2.Visible =
true;
textBox2.Visible =
true;
}
public static void Main(string[] args)
{
Form1 f1 =
new Form1();
Application.Run(f1);
}
}
}


Similar Articles