I use the following code to download a file from an http server:
int bytesSize = 0;
byte[] downBuffer = new byte[4096];
bool exceptionOccured = false;
try
{
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Timeout = 60000;
webRequest.ReadWriteTimeout = System.Threading.Timeout.Infinite;
webRequest.UseDefaultCredentials = true;
webResponse = (HttpWebResponse)webRequest.GetResponse();
Int64 fileSize = webResponse.ContentLength;
strResponse = webResponse.GetResponseStream();
strLocal = File.Create(destFilePath);
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?
source
share