如何通过http下载一个文件并保存到本地

trashfire 2003-07-16 06:24:20
private void getInternetFile(string url)
{
Uri fileUrl = new Uri(url);
// Create a 'FileWebrequest' object with the specified Uri.
WebRequest myWebRequest = (WebRequest)WebRequest.Create(fileUrl);
// Send the 'FileWebRequest' object and wait for response.
WebResponse myWebResponse = (WebResponse)myWebRequest.GetResponse();
// Get the stream object associated with the response object.
FileStream receiveStream = myWebResponse.GetResponseStream();

BinaryReader r = new BinaryReader(receiveStream);
// Release resources of response object.
myWebResponse.Close();
}
以上是我的代码,访问url并保存到本地固定目录下,写不下去,希望哪位能帮帮。
...全文
211 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Delon 2003-10-27
  • 打赏
  • 举报
回复
dfghdsfghdfghdfghdfg
dahuzizyd 2003-07-16
  • 打赏
  • 举报
回复
两种方法:
1.把文件保存到磁盘上:
WebClient Client = new WebClient();
Client.DownloadFile("http://www.aaa.com/index.asp","index.htm");
2.把数据从数据流提取到内存中:
WebClient Client = new WebClient();
Stream strm = Client.OpenRead("http://www.aaa.com/index.asp");
孟子E章 2003-07-16
  • 打赏
  • 举报
回复
看是否是权限问题

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);
孟子E章 2003-07-16
  • 打赏
  • 举报
回复
看是否是权限问题

110,535

社区成员

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

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

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