给出一个网址如何用asp.net将网页下载到我的硬盘
e.g。如果您在ie6中打开网址http://www.cnn.com并使用文件另存为,则会将html网页下载到您的系统。
我如何通过asp.net实现这一目标
答案 0 :(得分:3)
正如womp所说,在我看来,使用WebClient更简单。这是我更简单的例子:
string result;
using (WebClient client = new WebClient()) {
result = client.DownloadString(address);
}
// Just save the result to a file or do what you want..
答案 1 :(得分:1)
这应该可以胜任。但是,如果从ASP.NET页面中执行此操作,则需要考虑安全性。
public static void GetFromHttp(string URL, string FileName)
{
HttpWebRequest HttpWReq = CreateWebRequest(URL);
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Stream readStream = HttpWResp.GetResponseStream();
Byte[] read = new Byte[256];
Stream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
int count = readStream.Read(read, 0 , 256);
while (count > 0)
{
fs.Write(read, 0, count);
count = readStream.Read(read, 0, 256);
}
readStream.Close();
HttpWResp.Close();
fs.Flush();
fs.Close();
}
答案 2 :(得分:0)
WebClient client = new WebClient();
Stream data = client.OpenRead ("http://www.myurl.com");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine (s);
data.Close();
reader.Close();
答案 3 :(得分:0)
String url = "http://www.cnn.com";
var hwr = (HttpWebRequest)HttpWebRequest.Create(url);
using (var r = hwr.GetResponse())
using (var s = new StreamReader(r.GetResponseStream()))
{
Console.Write(s.ReadToEnd());
}