【发布时间】:2011-09-30 10:01:06
【问题描述】:
遇到问题,第一次调用 WebRequest.GetResponse() 时挂起并超时,但在第一次调用后,一切正常。
try {
WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.x.x/");
// Sends the HttpWebRequest and waits for the response.
myHttpWebRequest.Timeout = 1000;
WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
} catch(Exception e) {
Console.WriteLine("Failure 1");
}
try {
WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.x.x/");
// Sends the HttpWebRequest and waits for the response.
myHttpWebRequest.Timeout = 1000;
WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
} catch(Exception e) {
Console.WriteLine("Failure 2");
}
try {
WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.x.x/");
// Sends the HttpWebRequest and waits for the response.
myHttpWebRequest.Timeout = 1000;
WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
} catch(Exception e) {
Console.WriteLine("Failure 3");
}
在控制台应用程序中使用此代码,我总是收到Failure 1。是否在调试器下运行。我做了一个 1000 循环,它总是在第一个循环中失败,从没有其他循环。事实上,读取 Web 服务器的日志,它实际上从来没有收到过第一个请求。我在这里遗漏了什么吗?
【问题讨论】:
-
你有没有得到这个问题的解决方案'
-
是的,这是运行 VPN 的结果。关闭 VPN 或类似 VPN 的软件解决了这个问题。
标签: c# .net httpwebrequest webrequest