我创建了一个程序来调用数千个 url,我想做的是确保 url 超时我可以将超时错误追溯到 url。我所拥有的是:
string url = "http://www.google.com";
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
//From Fiddler
DateTime giveUp = DateTime.UtcNow.AddSeconds(5);
if (DateTime.UtcNow > giveUp)
throw new TimeoutException("The following url could not be reached: " + url);
我想制作一个单元来测试超时异常是否正常工作。如果您需要更多详细信息,请告诉我,但这应该足够了。