Handling an Internet Connection Lost During File Download Using HTTP

I use the following code to download a file from an http server:

        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;
        }

The problem is that at boot time, when the internet connection is disconnected. The control gets stuck in a while loop, and it continues to read and write. It never throws any exceptions or error messages. I want to handle the case when the Internet connection breaks during boot. What is missing or not here?

+4
source share
2 answers

Well for me below are possible steps

1. NetworkChange.NetworkAvailabilityChanged http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx, , : 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;
+1

TCP MSDN

+1

Source: https://habr.com/ru/post/1533658/


All Articles