gpt4 book ai didi

c# - 使用 HTTP 处理文件下载期间丢失的 Internet 连接

转载 作者:太空狗 更新时间:2023-10-29 21:54:08 25 4
gpt4 key购买 nike

我正在使用以下代码从 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;
}

问题是在下载过程中,当互联网连接断开时。控件卡在 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
}

事件的订阅可以如下完成

NetworkChange.NetworkAvailabilityChanged += this.NetworkAvailabilityChanged;

关于c# - 使用 HTTP 处理文件下载期间丢失的 Internet 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22679053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com