Hi all,
As of to my project requirement i have to read the source of a webpage and i have to store the source in a txt document.As i have to retrieve some values of the page from its source and display in my website.And the above functionality has to done in c#.So, anybody can please help me in the above concept can reply back.
Thanks in advance
David MortonPosted Jan 9, 2009, 5:20 PM
You'll need to create a web request, and make a request out to the internet to fetch the website. This is done by calling "GetResponse()", which returns a WebResponse object, which you will cast into an HttpWebResponse, and then call GetResponseStream() on. The result of GetResponseStream(), will be a stream object, which you can then use to instantiate a StreamReader, and read the text.
To write the text out to a file, call File.WriteAllText.
Here's an example:
static void WriteSourceToFile(string url, string filename)
{
WebRequest request = HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string documentText = reader.ReadToEnd();
reader.Close();
File.WriteAllText(filename, documentText);
}