机器特定的HttpWebResponse超时 [英] Machine-Specific HttpWebResponse Timeout
问题描述
我有一个非常奇怪的机器特定问题.在某些计算机上,下面的代码有效,在其他计算机上,它冻结,直到GetResponse()调用引发超时异常.
I have a very odd machine-specific issue. On some machines, the code below works, on others it freezes until a timeout exception is thrown on by GetResponse() call.
string url = "https://myserver/myimage.png";
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) =>
{
return true;
});
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.AllowAutoRedirect = false;
try
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine("Success");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
Console.ReadKey();
我无法在运行的机器和发生故障的机器之间找到任何操作系统/配置模式.这是一个与管理员用户一起运行的控制台应用程序.
I can't find any OS/configuration pattern between the machines that work and the ones that fail. This is a console application running with an administrator user.
唯一需要注意的是,访问的URL是运行在使用自发SSL证书的测试服务器中的映像,因此它需要使用上面代码中显示的ServerCertificateValidationCallback进行解决.
The only particular thing about this is that the URL being accessed is an image running into a test server that is using a self-issued SSL certificate, so it needed the workaround using ServerCertificateValidationCallback displayed in the code above.
我测试过的其他URL正常工作.
Other URLs I tested worked properly.
有人可以帮助我确定此问题的可能原因和解决方法吗?
Can anyone help me identify possible causes for this issue and workarounds?
推荐答案
和往常一样,一旦弄清楚,这很简单:不存在问题的计算机已安装.Net Framework 4.5.
As usual, it's quite simple once you figure it out: The machines where the problem was not present had .Net Framework 4.5 installed.
这很有可能是由于4.5是就地更新 for 4.0,并包含几个对核心库的错误修正.
This is very likely due to the fact that 4.5 is an in-place update for 4.0, and includes several bugfixes to the core libraries.
这篇关于机器特定的HttpWebResponse超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!