C#用WebClient下载File时操作超时的问题

今天很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();//及时释放,避免第二次下载时, 奇怪的"操作超时"的问题

 

 

posted on 2012-09-26 17:24  leegool  阅读(7583)  评论(3编辑  收藏  举报

导航