TA的每日心情 | 擦汗 2022-8-30 09:02 |
---|
签到天数: 2 天 连续签到: 2 天 [LV.1]测试小兵
|
- private void LoadData()
- {
- string BoardStream=null;//下载内容存入此变量
- Uri url = new Uri("http://www.163.com");//将下载地址转换为Uri类型
- HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(url);
- WebResponse response = requestPage.GetResponse();
- Stream stream = response.GetResponseStream();//获取页面流
- if (response.ContentType.ToLower().StartsWith("text/"))//如果获得成功(即为文本格式)
- {
- StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//读取获得内容流
- BoardStream = reader.ReadToEnd();//将内容流转换为文本并存入变量BoardStream,即为所需要的数据流
- }
- // string path=Server.MapPath ();
- StreamWriter saveAPage = new StreamWriter("a.html", false, System.Text.Encoding.GetEncoding("gb2312"));//实例化写入类,保存路径假设为C:/a.html
- saveAPage.Write(BoardStream);//创建写入任务
- saveAPage.Flush();//写入文件(即清理缓存流)
- saveAPage.Close();//关闭写入类的对象
- }
复制代码
|
|