惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
U
Unit 42
B
Blog RSS Feed
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
腾讯CDC
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - 聂微东
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta

博客园 - leegool

常见XSD问题 EntitySpace 常用语句 抗投诉空间 用C# 实现 Zen Cart 的用户密码加密算法 ASP.NET MVC 3 新特性 ASP.NET MVC 局部缓存实现 用户控件缓存 Partial Output Caching - leegool System.Web.Security.SqlMembershipProvider”要求一个与架构版本“1”兼容的数据库架构。 关于MarshalByRefObject的解释 MVC upload image MVC上传图片的例子 - leegool jQuery Custom Selector JQuery自定义选择器 - leegool 清楚浏览器缓存 Overview of Full Text Stop Words(MSSQL全文索引的干扰词概括)MSSQL 全文索引的最小单词长度 JQuery性能优化 MS SQL 事务隔离级别 认识ASP.NET 中的 AppDomain 国外服务器上 中文成 显示乱码 Sharing cookie Across domain 跨域cookie访问 cookie跨域 web性能优化(最佳) EntitySpaces 2009 Trial Expire 异常
C#用WebClient下载File时操作超时的问题
leegool · 2012-09-26 · via 博客园 - leegool

今天很SB,被这个问题卡住了。那段代码也是网上找的。结果发现只能下载一个文件,第二次下载的时候就会出现“操作超时”的问题。

这个是原代码:

System.Net.WebClient wc = new System.Net.WebClient();

wc.OpenRead("http://patrickkroft.com/mp3/Pearl.mp3");

Int64 bytes_total= Convert.ToInt64(wc.ResponseHeaders["Content-Length"])

MessageBox.Show(bytes_total.ToString() + " Bytes");

以上的代码实在是很不负责,坑爹的写的。改进成下面:

System.Net.WebClient wc = new System.Net.WebClient();

Stream stream = wc.OpenRead(folderEntity.URI);
Int64 bytes_total = Convert.ToInt64(wc.ResponseHeaders["Content-Length"]);
stream.Close(); //以及释放内存
wc.Dispose();//及时释放,避免第二次下载时, 奇怪的"操作超时"的问题