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?
For handling the download of the file, the download attribute could be a solution (with polyfill ). But for the handling the HTTP errors in my opinion the best solution is using some back-end proxy which will check the HTTP headers and also will force the browser to download a specific file.
So, updating your network drivers could tackle the "downloads keep failing network error" on Chrome. To get started, type Device Manager in the Windows Start Menu and select Device Manager when it appears. Select the Network adapters option. Right-click on your PC's network driver and select Update driver.
However, such features can sometimes go overboard by blocking all downloads—even if they aren't harmful. So, disabling HTTPS scanning or temporarily disabling your antivirus program could help. But then, remember to re-enable HTTPS scanning or your antivirus program when you finish downloading. 3. Try Incognito Mode
If you have a loss of connection you don't get a response code, you get no response at all. Response codes (like 200, 404, 500, etc) come directly from the server.
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;
Refer this
TCP alive from MSDN documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With