download webpage by asp.net [closed]

谁都会走 提交于 2019-12-23 04:59:05

问题


given a url how can i download the webpage to my harddrive with asp.net

e.g. if you open the url http://www.cnn.com in ie6 and use file save as, it will download the html page to your system.

how can i achieve this by asp.net


回答1:


As womp said, using WebClient is simpler in my opinion. Here is my simpler example :

string result;
using (WebClient client = new WebClient()) {
    result = client.DownloadString(address);
}
// Just save the result to a file or do what you want..



回答2:


This should do the job. But you will need to consider security if you are doing it from within an ASP.NET page.

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();
        }



回答3:


Use System.Net.WebClient.

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();



回答4:


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());
}


来源:https://stackoverflow.com/questions/1371406/download-webpage-by-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!