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/fi开发者_如何学编程nance";
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;
}
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();
}
}
精彩评论