Screen Scrap of Web Page

How about getting the contents of  url and set it in your own page? Well, you can use ASP.net to do that. Here is the codebehind code in C#

using System;
using System.Net;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class tv_scrap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
String strURL=txtUrl.Text;
String strHTML =String.Empty;

HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
objWebRequest.Method = “GET”;
HttpWebResponse objWebResponse = (HttpWebResponse)objWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(objWebResponse.GetResponseStream());

strHTML = streamReader.ReadToEnd();
txtResponse.Text = strHTML;
lblResponse.Text = strHTML;

streamReader.Close();
objWebResponse.Close();
objWebRequest.Abort();
}
}

and the HTML code,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="scrap.aspx.cs" ValidateRequest="false" Inherits="tv_scrap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Screen Scraping</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtUrl" runat="server" ></asp:TextBox>
    <asp:Button ID="btnSubmit" Runat="server" Text="Scrape Me!" OnClick="btnSubmit_Click" />
    <br/>
    <asp:TextBox ID="txtResponse" Runat="server" Width="800" Height="300" TextMode="MultiLine" />
    <br/>
    <asp:Literal ID="lblResponse" Runat="server" />
    </div>
    </form>
</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s