3

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?

Roberto
  • 494
  • 11
  • 23

1 Answers1

0

It seems that this is a bug in webproxy auto-detection.

By the way I found a more elegant workaround that allows me to use my initial code. So after reading this and this questions I have put this code in the App.config file

<system.net>
  <defaultProxy enabled="true" useDefaultCredentials="true" >
      <proxy autoDetect="True" scriptLocation="http://wpad/wpad.dat"/>
  </defaultProxy>
</system.net>

This allows the program to skip the internet option settings and go staight to check the wpad (similarly as firefox does)

Community
  • 1
  • 1
Roberto
  • 494
  • 11
  • 23