Archive

Posts Tagged ‘WebRequest’

Simple Screen Scraping in C#

January 26th, 2009

In C# Microsoft has provided HttpWebRequest and HttpWebResponse classes for the screen Scraping task. you have to write only few lines to get the work done.

String URL = @”www.abcd.com”;
// Prepare the HttpWebRequest
System.Net.HttpWebRequest htpreq = (HttpWebRequest)System.Net.WebRequest.Create(URL); 
// Define Method Get
htpreq.Method =“GET”; 
// Get the Response

System.Net.WebResponse htpRes = htpreq.GetResponse(); 

// Read the Response Stream
System.IO.StreamReader sr = new System.IO.StreamReader(htpRes.GetResponseStream()); 
 //get the response in string format.
String result = sr.ReadToEnd();
// Create a text File name test.text
System.IO.StreamWriter file = System.IO.File.CreateText(”test.text”);
// write the results in the file
file.Write(result);
// Close the File
file.Close();


Done.

C# , , , , ,