Code stops working in the middle of process with no errors

I have a function :

public bool urlExists(string url)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                request.Method = "HEAD";

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                Console.WriteLine("false");
                return false;
            }
        }

Which checks if url exists, or not. And I have another function to download files.

    public void downloadImages(string imgCode)
        {
    using (WebClient wc = new WebClient())
                {
                    try
                    {
                        if (urlExists("mydomain.com/images/" + imgCode + "/large.png"))
                        {
=                            wc.DownloadFile("mydomain.com/images/" + imgCode + "/large.png", "filepath" + imgCode + ".png");
                        }
                        if (urlExists("mydomain.com/images/" + imgCode + "/large.jpg"))
                        {
                            wc.DownloadFile("mydomain.com/images/" + imgCode + "/large.jpg", "filepath" + imgCode + ".jpg");
                        }
                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
           }

I have a list of 'imgCodes' and I call the function like this:

for (int i = 0; i < imgCodes.Count; i++)
            {
                downloadImages(imgCodes[i]);
            }

The program starts working, but in the middle of the process, it stops and it exists the thread for some reason. I tried adding some lines to find out where the problem occurs, but I couldn't find out.. but the closest I got is that it might have to do something with switing from one to another format.

For example, if it downloads a png, and next image is a jpg, it stops working and exists the thread. Or if it downloads a jpb and next image is png, it stops working.

Jon Skeet
people
quotationmark

You're not disposing of your responses, so you're never returning the connections to the connection pool - if you're fetching multiple images from the same host, the connection pool is preventing you from opening more connections to that host, waiting for the existing ones to be returned. Just make sure you dispose of the response:

using (var response = (HttpWebResponse) request.GetResponse())
{
    return response.StatusCode == HttpStatusCode.OK;
}

Although I have to say, it seems pointless to make two requests per image - one just to check whether or not it exists and then another to get the actual data. I would just try to download each file, and handle the case where the download fails due to it not existing.

people

See more on this question at Stackoverflow