Thursday 10 May 2012

Beginning .Net , C# Tips : Simple Screen Scrap using c# and vb.net

Screen scrapping means to get other sites data from given URL.
You can do this using the HttpWebRequest and HttpWebResponse classes to screen scrape, you can use the following code to build a Web page that will serve as a simple Web browser. You also learn how to display another Web page inside of yours using an HttpWebRequest. In this example, you scrape the "http://msdn.microsoft.com/en-US/" URL and display it in a panel on your Web page.
For this example we are using System.Net and System.IO Namespaces.

ASPX Code : 
 <asp:Panel runat="server" ID="pnlScreen" ScrollBars=Auto
Width="800px" Height="500px"></asp:Panel>

C# Example :
Uri url = new Uri("http://msdn.microsoft.com/en-US/");
if (url.Scheme == Uri.UriSchemeHttp)
{
    //Create Request Object
    HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    //Set Request Method
    objRequest.Method = WebRequestMethods.Http.Get;
    //Get response from requested url
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    //Read response in stream reader
    StreamReader reader = new StreamReader(objResponse.GetResponseStream());
    string tmp = reader.ReadToEnd();
    objResponse.Close();
    //Set response data to container
    this.pnlScreen.GroupingText = tmp;
}

The HttpWebRequest to the "http://msdn.microsoft.com/en-US/" page returns a string containing the scraped HTML. The sample assigns the value of this string to the GroupingText property of the Panel control. When the final page is rendered, the browser renders the HTML that was scraped as literal content on the page.

VB.net Example :
Dim url As New Uri("http://msdn.microsoft.com/en-US/")
If url.Scheme = Uri.UriSchemeHttp Then
    'Create Request Object
    Dim objRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    'Set Request Method
    objRequest.Method = WebRequestMethods.Http.[Get]
    'Get response from requested url
    Dim objResponse As HttpWebResponse = DirectCast(objRequest.GetResponse(), HttpWebResponse)
    'Read response in stream reader
    Dim reader As New StreamReader(objResponse.GetResponseStream())
    Dim tmp As String = reader.ReadToEnd()
    objResponse.Close()
    'Set response data to container
    Me.pnlScreen.GroupingText = tmp
End If
Output :
(To view original image , click on image)

No comments:

Post a Comment