1

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.

3 Answers 3

2

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

You should take a look at the WebClient Class

An example can be found on the link posted above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.