2011-03-22 44 views
1

我是ASP新手,想知道是否有方法将网页源代码保存为字符串变量或.txt文件一个使用C#或ASP.net与C#的网站地址。C#和ASP.net将html保存为字符串或文件

如果可能的话,示例代码和关于要引用的库的信息会非常有用。

回答

2

您可以使用该WebClient类:

给一个字符串变量:

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

到文件:

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

没问题:

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

HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse; 

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

在一个基地ic高水平。

0

你应该看看WebClient Class

一个例子可以在上面发布的链接上找到。

相关问题