I use the following code to check the status of about 20 internal and external websites to know when they are returning errors. Lately it will run on 15 sites then on the 16th, it will just hang there. If I try to the open the website that it had an issue on, it keeps loading and never finishes. If I wait a few minutes it might work.
Am I sending too many requests?
Is my code causing the website to become unresponsive?
Is there a better way to get the status code for a website?
private int getWebRequestValue(string website)
{
try {
WebRequest request = WebRequest.Create(website);
using (WebResp开发者_StackOverflow中文版onse response = request.GetResponse()) {
HttpWebResponse httpResponse = (HttpWebResponse)response;
return httpResponse.StatusCode;
}
} catch (WebException e) {
using (WebResponse response = e.Response) {
if (response == null) {
return 1;
} else {
HttpWebResponse httpResponse = (HttpWebResponse)response;
return httpResponse.StatusCode;
}
}
}
}
NOTE
I cannot use a company to do the website monitoring
UPDATE After using fiddler while running my app I can see that the websites that aren't loading are getting a 302 status code
I don't see any way your code would be causing the website to hang. Also, this seems like a good way to get the status code. What you probably need to do is add a timeout, say 30 seconds, to your webrequest. WebRequest.Timeout is the property, more info @ http://msdn.microsoft.com/en-us/library/system.net.webrequest.timeout.aspx.
精彩评论