HttpWebRequest and Multiple Files

I’m working on a project at work right now to pull inventory data from the bookstore’s point-of-sale system into the e-commerce site I’m setting up for them. One of the parts in the process is to check the web server for any new/updated images for the items on the website. I am doing a web request for each image on the server to see if I really does exist. After doing so many, I noticed that all of the files kept coming back saying they weren’t there, when I know darn well they are! So after a little research online, I noticed that you need to close your Response object when you are finished with it.

Here’s my code:

private static bool _FileExists(string filename) {
    try {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(filename);
        req.Timeout = 5000;
        req.Method = "HEAD";
        HttpWebResponse response = (HttpWebResponse)req.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK) {
            response.Close();    // IMPORTANT
            return true;
        } else {
            response.Close();    // IMPORTANT
            return false;
        }
    } catch(Exception ex) {
        return false;
    }
}

The big thing to note here is when I’m calling response.Close(), as that closes out the connection. So if you are experiencing issues where you keep getting time out errors, give this a try. Here’s the original article that helped me.

Windows Mobile Event Viewer

Well, I started in on a new project last night. This project will essentially be an event viewer for windows mobile devices. One of my concerns is that it won’t log events from the device…I’m not exactly sure how to hook in to device events yet. I can’t guarantee that this application will be running all the time to record device events, so I am thinking mainly of using it for application developers that want to use it to record events from their programs. They will be able to call a
method and pass all the information to create a new event in the event log.

I’ll post something worthwhile when it gets a little farther.