警惕使用WebClient.DownloadFile(string uri,string filePath)方法
原文:警惕使用WebClient.DownloadFile(string uri,string filePath)方法

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();
    }
}

posted on 2014-11-06 02:15 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/4077754.html

警惕使用WebClient.DownloadFile(string uri,string filePath)方法相关推荐

  1. 不建议使用类型 Activity 的方法 managedQuery(Uri, String[], String, String[], String)解决方案

    问题: managedQuery方法在2.3.3没问题,但是在4.2.2里面就不行了,提示:The method managedQuery(Uri, String[], String, String[ ...

  2. WebClient DownloadFile 用法

    using (WebClient myWebClient = new WebClient()){try{myWebClient.DownloadFile(new Uri(href), System.I ...

  3. Java时间日期格式转换Date转String和String转Date

    Java时间格式转换大全 谢谢大神的时间格式转换稿子:https://blog.csdn.net/u010486495/article/details/79260448 查阅后发现没有我想要的,后经人 ...

  4. static string java_java:String类、static关键字、Arrays类、 Math类

    public voidStrMethodTest(){/** public int length () :返回此字符串的长度. * public String concat (String str) ...

  5. java string.indexof(string)_Java StringBuffer indexOf()方法

    Java StringBuffer indexOf()方法 java.lang.StringBuffer.indexOf(String str, int fromIndex) 方法返回此字符串指定的子 ...

  6. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str="This is a String"; int len =str.le ...

  7. 是否有一个不区分大小写的string.Replace的替代方法?

    本文翻译自:Is there an alternative to string.Replace that is case-insensitive? I need to search a string ...

  8. 转换ArrayList String 到String []数组[重复]

    本文翻译自:Convert ArrayList to String[] array [duplicate] This question already has an answer here: 这个问题 ...

  9. C#中的String和string有什么区别?还有ref关键字怎么用?

    String 和 string 没有区别 string 是 String 的别名 ref 关键字一般只用于函数参数中,这个参数是 struct 类型,而不是 class 类型 因为 class 类型在 ...

最新文章

  1. sdr 软件_SDR 软件定义的无线电
  2. 成功让Eclipse更新ADT的方法
  3. mysql访问oracle数据_python访问Oracle及Mysql数据库
  4. Android Studio之Instant Run requires ‘Tools | Android | Enable ADB integration‘ to be enabled解决办法
  5. 次世代角色建模的学习流程,软件学习顺序
  6. mysql 设置数据库只读_如何设置mysql数据库为只读
  7. matlab中怎么查看变量,Matlab 查看内存中的变量,清空屏幕等命令
  8. 如何优雅的统计代码耗时?
  9. 甘特图控件VARCHART XGantt,分析拖动工具!
  10. 湘潭大学计算机组成原理试卷,湘潭大学11级原理课堂测验题集.ppt
  11. 运用java打印出菱形
  12. 【深度学习】激活函数:原理+常见激活函数(Sigmoid\Tanh\ReLU\Leaky ReLU\Softmax)
  13. 拉格朗日插值法与牛顿插值多项式
  14. 6.stm32 低功耗设计--总结
  15. 风速Weibull分布和光伏Beta分布的参数拟合方法
  16. LTSPICE使用教程:参数变量和参数扫描
  17. 读real time localization and 3D reconstruction笔记
  18. 欧盟森林砍伐法规和合规性:使用 Dimitra 技术解决森林砍伐问题
  19. linux 程序开机重启电脑,Linux开机命令和重启命令表明
  20. 京东商城的融资诱惑与亏损陷阱

热门文章

  1. Android开发工具之Android Studio---版本控制SVN使用三(常规操作)
  2. python显示数据长度_Python数据分析之初识可视化
  3. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林
  4. JZOJ 3852. 【NOIP2014八校联考第2场第2试9.28】单词接龙(words)
  5. iphone看python文件_Python实战 | 只需 ”三步“ 爬取二手iphone手机信息(转发送源码)...
  6. BZOJ-3190-赛车-JLOI2013-暴力枚举
  7. BZOJ-3110-K大数查询-ZJOI2013-整体二分
  8. Codeforces 1106F Lunar New Year and a Recursive Sequence (数学、线性代数、线性递推、数论、BSGS、扩展欧几里得算法)...
  9. java实现分发_关于JAVA中事件分发和监听机制实现的代码实例
  10. 软工作业6--用户体验(案例分析)