Reader Level:
ARTICLE

Reading and Display Source of Web Pages

Posted by sushilsaini Articles | ASP.NET Programming December 30, 2003
This sample project is made to help ASP.NET developers. This projects highlights how to add HTML page inside your ASP.NET page which is hosted on same or different webserver. This page can still run on that server and only output is included in the ASP.NET page.
  • 0
  • 0
  • 17230
Download Files:
 

Description :

This is a sample project made to help ASP.net developers. This projects highlights how to add HTML page inside your asp.net page which is hosted on same or different webserver. This page can still run on that server and only output is included in the asp.net page. 

Introduction

This code is developed to short out the problems of the include files in ASP.net applications. Developers faces frequent problems of including HTML code in the page which is output of an ASP. These output can carry different content depending on the various factors like query string variable or sessions or cookies.

This is a easy to use as only with few lines of code you will be able provide dynamism to your applications.

Table Format

Heading Heading
Reading/Display source of web pages This code is developed to short out the problems of the include files in ASP.net applications. Developers faces frequent problems of including HTML code in the page which is output of an ASP. These output can carry different content depending on the various factors like query string variable or sessions or cookies.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

ASPX page contains one textbox , Button and Panel control on the page

<form id="default" method="post" runat="server">
Enter Url : <asp:TextBox id="txt_url" runat="server"></asp:TextBox> eg. http://yahoo.com
<asp:Panel id="mainpanel" runat="server">PLACE SOURCE </asp:Panel>
<asp:Button id="Button1" runat="server" Text="Load Source"></asp:Button>
</form>

On the button click event following function is used :

public void MySource(Uri weburi)
{
System.Text.StringBuilder sbuild=
new StringBuilder();
string temp="";
try
{
System.Net.HttpWebRequest webrequest = (HttpWebRequest)System.Net.WebRequest.Create(weburi);
System.Net.HttpWebResponse webresponse=(HttpWebResponse) webrequest.GetResponse();
StreamReader webstream =
new StreamReader(webresponse.GetResponseStream(),Encoding.ASCII );
while((temp=webstream.ReadLine())!= null)
{
sbuild.Append(temp + "\r\n");
}
webstream.Close();
Response.Write("OK");
this.mainpanel.Controls.Add(new System.Web.UI.LiteralControl(sbuild.ToString() ));
}
catch(WebException wex)
{
Response.Write (wex.Message );
}
}

//This created instance of String Builder object to store the information.
System.Text.StringBuilder sbuild=new StringBuilder();
// Initialize the WebRequest.
System.Net.HttpWebRequest webrequest = (HttpWebRequest)System.Net.WebRequest.Create(weburi);
// Return the response.
System.Net.HttpWebResponse webresponse=(HttpWebResponse) webrequest.GetResponse();
//The GetResponseStream method returns the data stream from the Internet resource.
StreamReader webstream = new StreamReader(webresponse.GetResponseStream(),Encoding.ASCII );

Note The response stream must be closed to avoid running out of system resources. The response stream can be closed by calling Stream.Close or Close

//This loop reads the content from reader and stores in temp varable . temp add the string to string builder instance sbuild
while((temp=webstream.ReadLine())!= null) { sbuild.Append(temp + "\r\n"); }
// Close the response to free resources.
webstream.Close();
//string value carried by sbuild is passed to literal control (Represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.
this.mainpanel.Controls.Add(new System.Web.UI.LiteralControl(sbuild.ToString() ));

We have added this code to button Click event .You can use this on the page load also.

private void Button1_Click(object sender, System.EventArgs e)
{
string uri=this.txt_url.Text ;
System.Uri weburi =
new System.Uri(uri,false);
MySource(weburi);
}

To customize your pages , you can put several panels on your pages and load them using this techniques with asp/ HTML/ anyother pages web content as include files.

To test this page  type http://www.yahoo.com in the text box and click the button given on the page and wait for the page response.

Summary

This project is development using following packages in the ASP.net Application with Csharp.

COMMENT USING

Trending up