I have the following code
private bool IsOnline()
{
try
{
var wr = WebRequest.CreateHttp("http://www.google.com");
wr.KeepAlive = false;
wr.Credentials = CredentialCache.DefaultCredentials;
using (wr.GetResponse())
{
return true;
}
}
catch
{
return false;
}
}
When I execute it it remains stuck on GetResponse line forever.
Thanks to the responses we found that the problem is in the default proxy. In fact if I construct a new proxy in the following way it works
var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", false);
var proxy = (string)registry.GetValue("ProxyServer");
var isProxyEnabled = (int)registry.GetValue("ProxyEnable");
if (isProxyEnabled > 0)
{
wr.Proxy = new WebProxy(proxy, true, null, System.Net.CredentialCache.DefaultNetworkCredentials);
}
The problem is that this workaround code read from the registry the proxy that was manually set. It doesn't works if the user have choose "Automatically detect settings".
so: - how can be found the proxy address in that case? - why the default proxy does not work?