Machine-Specific HttpWebResponse Timeout

允我心安 提交于 2019-12-24 03:40:12

问题


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.

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.

Other URLs I tested worked properly.

Can anyone help me identify possible causes for this issue and workarounds?


回答1:


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.

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.




回答2:


I've had the same issue on Win7 , VS2010 and it was solved by windows update - update to .NET 4.5.1
Another thing worth mentioning that it happens only in specific servers as well , not only specific clients.
(i would post as comment but not enough ratings)
Did anyone solve this without using windows update ?



来源:https://stackoverflow.com/questions/14221352/machine-specific-httpwebresponse-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!