I wrote this function to check if an internet connection is available:
bool IsOnline()
{
try
{
var request = (HttpWebRequest)WebRequest.CreateHttp("http://www.google.com/");
request.Timeout = 2000;
var response = (HttpWebResponse)request.GetResponse();
return ((int)response.StatusCode) < 400;
}
catch (Exception) { return false; }
}
It seems to work in almost all cases however under my work network it return false after a timeout error while the connection is available.
Note:
– this function return false but I can go online with a webbrowser component in my WPF application
– the connection is pretty good. (so it is impossible to spend more than 2sec in loading google.com)
– I’m behind a proxy configured correctly in Control Panel/Internet Options/Connection
Any ideas?
As workaround I changed my function in this way:
If I try to use WebRequest.GetSystemWebProxy() I get a IWebProxy (WrappedWebProxy) that has the same settings as the one that I manually build but it does not work. It recognize all addresses as local also if I do not have check the “Bypass proxy server for local addresses”.
Then if I change through the debugger the bypasslocal flag it works. It’s so strange that looks like a bug.