Question

I'm programing an app for Microsoft PixelSense, and I'm using WPF for developing the user interface.

The app needs to download some content from the internet. It should be able to cope with a situation when the internet connection suddendly breaks while downloading. So when I need an internet connection, I got a try catch and catch every WebException or IOException due to the internet break.

Here's a snippet of my code :

System.Drawing.Image tmpimg = null;
Stream stream = null;
HttpWebResponse httpWebReponse = null;   

try
{
    // dowloading image
    HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(urlPicturesBig +urlresource);
    httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
    stream = httpWebReponse.GetResponseStream();
    tmpimg = System.Drawing.Image.FromStream(stream);
    // saving
    tmpimg.Save(@appDirectory + "\\resources\\" + urlresource);                                        
} 

catch (WebException)
{
    Debug.WriteLine("WebException");                       
    return -1;
}

catch (IOException) 
{
    Debug.WriteLine("IOException");
    return -1;                   
}

The problem is that my GUI breaks down when the IOException is handled (a list of buttons becomes grey colored). So I've tried to do things this way :

try
{
    // dowloading image
    HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(urlPicturesBig +urlresource);
    httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
}

catch (WebException)
{
    Debug.WriteLine("WebException");                       
    return -1;
}

stream = httpWebReponse.GetResponseStream();
tmpimg = System.Drawing.Image.FromStream(stream);

// saving
tmpimg.Save(@appDirectory + "\\resources\\" + urlresource);

But even if there's an internet break, the IOException is handled, and the programm doesn't read catch (WebException) instructions. If I remove try catch blocks, WebException is handled most of the time, sometimes it's the IOException .

Was it helpful?

Solution

The problem as you state is that a failed network fetch can end up corrupting the backup resources you want to use.

As discussed in the comments below, this is due to the exception being thrown as the image stream is fetched from the network during the write to disk. Using this kind of code should protect you against this. Needless to say, you should perform some sanity checking on the length of the stream that is being returned.

var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(urlPicturesBig + urlresource);
MemoryStream memory= new MemoryStream();
using (var httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
     var stream = httpWebReponse.GetResponseStream();
     //read the entire stream into memory to ensure any network issues
     //are exposed
     stream.CopyTo(memory);
}
var tmpimg = System.Drawing.Image.FromStream(memory);            {
tmpimg.Save(@appDirectory + "\\resources\\" + urlresource);    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top