我使用以下代码从http服务器下载文件:
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[4096];
bool exceptionOccured = false;
try
{
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Timeout = 60000;
webRequest.ReadWriteTimeout = System.Threading.Timeout.Infinite;
// Set default authentication for retrieving the file
webRequest.UseDefaultCredentials = true;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = webResponse.GetResponseStream();
// Create a new file stream where we will be saving the data (local drive)
strLocal = File.Create(destFilePath);
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
};
}
catch (WebException we)
{
exceptionOccured = true;
if (we.Status == WebExceptionStatus.NameResolutionFailure)
{
isExceptionOccured = true;
string errMsg = "Download server threw a NOT FOUND exception for the url:" + "\n" + url + "\nVerify that the server is up and running.";
MessageBox.Show(errMsg, "Cadence Download Manager", MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (we.Status == WebExceptionStatus.Timeout)
{
isExceptionOccured = true;
string errMsg = "Download server threw Timeout exception for the url:" + "\n" + url + "\nVerify that the server is up and running.";
MessageBox.Show(errMsg, "Cadence Download Manager", MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (we.Status == WebExceptionStatus.ProtocolError)
{
isExceptionOccured = true;
string errMsg = "Download server threw Timeout exception for the url:" + "\n" + url + "\nVerify that the server is up and running.";
MessageBox.Show(errMsg, "Cadence Download Manager", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
isExceptionOccured = true;
string errMsg = "Download server threw an unhandled exception for the url:" + "\n" + url;
MessageBox.Show(errMsg, "Cadence Download Manager", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (System.IO.IOException)
{
exceptionOccured = true;
string errMsg = "Unable to read data from the download server for the url:" + "\n" + url + "\nVerify that the server is up and running.";
isExceptionOccured = true;
}
catch (Exception)
{
exceptionOccured = true;
string errMsg = "Unable to read data from the download server for the url:" + "\n" + url + "\nVerify that the server is up and running.";
isExceptionOccured = true;
}
Run Code Online (Sandbox Code Playgroud)
问题是在下载过程中,当互联网连接断开时.控件卡在while循环中,并保持读写.它永远不会抛出任何异常或任何错误消息.我想处理下载过程中互联网连接中断的情况.这里缺少什么或错了什么?
根据我的说法,以下步骤是可能的
1.您可以利用NetworkChange.NetworkAvailabilityChanged事件http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx,它会告诉您LAN中出现问题的情况,例如:网线拔掉或者用户自己禁用了NetworkInterface。
2.如果出现网络中断,您需要对自己的服务器进行某种 ping 类型的机制来检查服务器是否可达,您可以在开始下载 ping 时启动计时器并定期检查直到下载完成,一旦下载完成或用户取消您可以停止计时器。
像下面这样
/// <summary>
/// Event handler for network availability changed event.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="eventArgs">Event arguments.</param>
private void NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs eventArgs)
{
////Log or mail
}
Run Code Online (Sandbox Code Playgroud)
事件的订阅可以如下完成
NetworkChange.NetworkAvailabilityChanged += this.NetworkAvailabilityChanged;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1843 次 |
| 最近记录: |