从url下载文件时,如果连接丢失并重置,则我的程序会抛出异常,如
“无法从传输连接中读取数据:连接 被关闭了“
这是我的代码。
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
WebClient client = new WebClient();
//Stream strRemote = client.OpenRead(url);
Stream strRemote = response.GetResponseStream();
FileStream strLocal =
new FileStream(sFileSavePath, FileMode.Create, FileAccess.Write, FileShare.None);
int iByteSize = 0;
byte[] byteBuffer = new byte[8192];
while (true)
{
try
{
iByteSize = strRemote.Read(byteBuffer, 0, byteBuffer.Length);
{
//some code related to saving into file.
}
}
catch (Exception ex)
{
MessageBox.Show("The Message: " + ex.Message);
}
}
当网络继续时,我的代码完美运行。但是当我丢失网络连接并且连接重置后我的程序显示异常。我想在网络连接重置后继续下载过程。请帮忙处理这个情况。