6

I am using the following code to download a file from 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 during download, when the internet connection goes off. The control is stuck in the while loop and it keeps reading and writing. It never throws any exception or any error message. I want to handle the case when internet connection breaks during download. What is missing or wrong here?

The King
  • 833
  • 1
  • 15
  • 41

2 Answers2

1

Well according to me below steps are possible

1.You can make use of NetworkChange.NetworkAvailabilityChanged event http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx which will tell you in case of problem in LAN, ex:network cable unplugged or the user itself disables NetworkInterface.

2.In case of Internet drop you need to have some ping kind of mechanism to your own server to check whether server is reachable or not, you could start a Timer when starting download ping and check periodically till the download completed, once download completed or user cancelled you can stop the timer.

Like below

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

and subscription of event can be done as below

NetworkChange.NetworkAvailabilityChanged += this.NetworkAvailabilityChanged;
Neel
  • 11,625
  • 3
  • 43
  • 61
1

Refer this

TCP alive from MSDN documentation

Community
  • 1
  • 1
LakshmiNarayanan
  • 1,158
  • 9
  • 12