似乎WebClient
没有关闭Response
它在完成后使用的对象,在您的情况下,这将导致同时打开许多响应,并且远程服务器上的连接限制为 25 个,您会收到“超时异常”。当您调试时,由于内部超时等原因,早期打开的响应会被关闭……(我WebClient
用反射器检查过,我找不到关闭响应的指令)。
我建议您使用HttpWebRequest & HttpWebResponse
,以便您可以在每次下载后清理对象:
HttpWebRequest request;
HttpWebResponse response = null;
try
{
FileStream fs;
Stream s;
byte[] read;
int count;
read = new byte[256];
request = (HttpWebRequest)WebRequest.Create(remoteFilename);
request.Timeout = 30000;
request.AllowWriteStreamBuffering = false;
response = (HttpWebResponse)request.GetResponse();
s = response.GetResponseStream();
fs = new FileStream(localFilename, FileMode.Create);
while((count = s.Read(read, 0, read.Length))> 0)
{
fs.Write(read, 0, count);
count = s.Read(read, 0, read.Length);
}
fs.Close();
s.Close();
}
catch (System.Net.WebException)
{
//....
}finally
{
//Close Response
if (response != null)
response.Close();
}