tags:

views:

42

answers:

1

Hi, I'm trying to write an application that gets stock quotes from Google Finance and graphs them, in C#. My program uses the HttpWebRequest class to request the source code for the web page containing the stock quote, about every thirty seconds. After getting the source, I then extract the quotes and graph them.

My issue is that when the program goes about requesting every 30 seconds, about 50% of the time I invoke "objWebReq.GetResponse()", I get timeout exceptions! I checked my internet connection, and I'm absolutely sure that I'm connected when the timeout exceptions occur. Is there something I'm not doing correctly, that would cause Google to occasionally reject my request? Here is the portion of my program that makes the request for the source code.

        //Create and configure object for request
        HttpWebRequest objWebReq = (HttpWebRequest)WebRequest.Create(url); //Valid URL
        objWebReq.Timeout = MSWAIT; //Set to 10 seconds
        objWebReq.Method = "GET";
        objWebReq.ContentType = "text/html";
        objWebReq.KeepAlive = false;
        objWebReq.Referer = "http://www.google.com/finance";
        objWebReq.UserAgent =
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115  
             Firefox/3.6";


        StreamReader objStream = null;


        //Create Object for the response.
        try
        {
            HttpWebResponse objWebResp = (HttpWebResponse)objWebReq.GetResponse();
            objStream = new StreamReader(objWebResp.GetResponseStream(),    
                                         System.Text.Encoding.ASCII);
        }
        catch
        {
         //Do Nothing
        }

        return objStream;
    }
+1  A: 

You're not disposing of the web response, which can lead to timeouts while the connection pool waits for a connection. I would suggest that you read the complete response within your method and return it as a string, so you can close the connection:

using (HttpWebResponse response = (HttpWebResponse)objWebReq.GetResponse())
{
    // Are you sure it's *really* ASCII, rather than (say) UTF-8?
    using (TextReader reader = new StreamReader(response.GetResponseStream(),
                                                Encoding.ASCII))
    {
        return reader.ReadToEnd();
    }
}
Jon Skeet
+1 for `using` and `encoding` bonus info!
RedFilter
I changed my code to accommodate the structure you described and it looks as if that fixed it. So far I've collected an hour's worth of data (120 source requests) without any time out exceptions. Thanks for your help!
Kevin Toste