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 b开发者_运维问答e very helpful.
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");
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.
You should take a look at the WebClient Class
An example can be found on the link posted above.
精彩评论