Aaron
Aaron

Reputation: 61

C# and ASP.net saving html into a string or a file

I'm new to ASP and I was wondering if there is a way to save the source of the web-page into a string variable or a .txt file given a website address using C# or ASP.net with C#.

If its possible, example code and information on what libraries to reference would be very helpful.

Upvotes: 1

Views: 3349

Answers (3)

Morten Anderson
Morten Anderson

Reputation: 2321

You should take a look at the WebClient Class

An example can be found on the link posted above.

Upvotes: 0

Tejs
Tejs

Reputation: 41246

Sure thing:

HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;

string html = new StreamReader(response.GetResponseStream()).ReadToEnd();

At a basic high level.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160902

You can use the WebClient class for that:

To a string variable:

string result;
using (WebClient wc = new WebClient())
    result = wc.DownloadString("http://stackoverflow.com");

To a file:

using (WebClient wc = new WebClient())
wc.DownloadFile("http://stackoverflow.com", @"C:\test\test.txt");

Upvotes: 2

Related Questions