如何保存来者网络的文件?

yarshray 2002-09-03 10:01:46
保存来者URL的网络文件
保存如:http://www.csdn.net的页面等..
好象不可以用流,
请问该用什么方法?
有关于Http/ftp之类的办法吗?
最好有详细代码!
...全文
35 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yarshray 2002-09-03
  • 打赏
  • 举报
回复
你的方法是获取流并读取这个我知道
我希望的是将读取的流保存到文件!
谢谢请问可以解答吗?
sailing_ms 2002-09-03
  • 打赏
  • 举报
回复
感谢您使用微软产品。

根据我的经验, 我们可以用 WebClient 类及其 DownloadFile 方法来实现这个要求.

如:
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath.);


更具体的说明和源码请参考下面这些资料:


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetWebClientClassTopic.asp

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemNetWebClientClassDownloadFileTopic.asp?frame=true


- 微软全球技术中心 技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
sinsky 2002-09-03
  • 打赏
  • 举报
回复
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.Default);
string html = sr.ReadToEnd();

这样就行了,或直接用resp.GetResponseStream()就可以获得流了

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧