开发者

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

开发者 https://www.devze.com 2023-02-18 20:44 出处:网络
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#.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消