警惕使用WebClient.DownloadFile(string uri,string filePath)方法
2008-08-05 14:22
483 查看
WebClient.DownloadFile(string uri,string filePath)方法用来请求一个url,并将请求内容存到本地的一个文件中。
使用这个方法,如果filePath是一个已经存在的文件,如果DownloadFile的执行web请求的过程中发生了错误,则会删除掉filePath以前的内容。以下是验证代码,和另一种选择方案。
class Program
{
static void Main(string[] args)
{
const string filePath = @"c:\a.html";
const string url = "http://dat0a11.book.hexun.com/chapter-1031-1-7.shtml";
try
{
using (WebClient wc = new WebClient())
{
//wc.DownloadFile("http://dat0a11.book.hexun.com/chapter-1031-1-7.shtml", filePath);
string html = wc.DownloadString(url);
using (StreamWriter writer = new StreamWriter(filePath,false,wc.Encoding))
{
writer.Write(html);
writer.Flush();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
使用这个方法,如果filePath是一个已经存在的文件,如果DownloadFile的执行web请求的过程中发生了错误,则会删除掉filePath以前的内容。以下是验证代码,和另一种选择方案。
class Program
{
static void Main(string[] args)
{
const string filePath = @"c:\a.html";
const string url = "http://dat0a11.book.hexun.com/chapter-1031-1-7.shtml";
try
{
using (WebClient wc = new WebClient())
{
//wc.DownloadFile("http://dat0a11.book.hexun.com/chapter-1031-1-7.shtml", filePath);
string html = wc.DownloadString(url);
using (StreamWriter writer = new StreamWriter(filePath,false,wc.Encoding))
{
writer.Write(html);
writer.Flush();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
相关文章推荐
- 警惕使用WebClient.DownloadFile(string uri,string filePath)方法
- c# WebClient.DownloadFile文件下载方法
- 使用WebClient.UpLoadFile方法在C/S程序中上传文件
- C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题
- 使用innodb_data_file_path修改数据文件位置,发生The server quit without updating PID file错误的解决原因及方法。
- 在Android系统中使用WebViewClient处理跳转URL的方法
- “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法
- WebClient.DownloadFileAsync实现下载网络数据-简单的下载软件
- “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法
- 利用HttpClient获得cookie,并在webview中使用的方法
- HttpUtility类,WPF 使用 .net framework 4.0 client profile 不能引用system.web 解决方法(UrlEncode,HtmlEncode)
- .NET的file文件上传控件使用方法 修改web.config文件上传大文件
- 使用fastdfs_client的storage.upload_file方法上传图片抛FileNotFound异常
- Csharp:WebClient and WebRequest use http download file
- .NET的file文件上传控件使用方法 修改web.config文件上传大文件
- .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类
- 在使用webview.loadData(String data, String mimeType, String encoding)方法时,可能有乱码
- WebClient.DownloadFile(线程机制,异步下载文件)
- String、Path、File、Directroy 常用方法总结
- spring mvc使用ClassPathXmlApplicationContext或FileSystemXmlApplicationContext和XmlWebApplicationContext类的操作其中 XmlWebApplicationContext是专为Web工程定制的。