asp.net 下载网页源代码

来源:互联网 发布:电视信源如何选择网络 编辑:程序博客网 时间:2024/05/16 12:12
//指定下载的文件名string fileName = "网页源代码.txt";//客户端保存的文件名//指定下载的url地址:http://www.baidu.comstring url = "http://www.baidu.com";WebClient client = new WebClient();用于传递url资源的方法client.Encoding = Encoding.UTF8;byte[] html = client.DownloadData(url);string strPath = MapPath("~/html");定义储存路径if(!Directory.Exists(strPath)){    Directory.CreateDirectory(strPath);}string savePath = Path.Combine(strPath,"html.txt");//下载文件的详细地址using(FileStream fsSave = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)){    fsSave .Write(html, 0, html.Length);}//义字符流的形式下载文件 FileStream fs = new FileStream(savePath, FileMode.Open);            byte[] bytes = new byte[(int)fs.Length];            fs.Read(bytes, 0, bytes.Length);            fs.Close();            Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));            Response.BinaryWrite(bytes);            Response.Flush();            Response.End();
0 0
原创粉丝点击