C# auto detect proxy settings

后端 未结 8 710
遇见更好的自我
遇见更好的自我 2020-12-01 06:37

C# 2008 SP1

I am using the code to detect if a proxy has been set under \"Internet Options\". If there is a proxy then I will set this in my webclient.

So I

相关标签:
8条回答
  • 2020-12-01 07:10

    Check out the System.Net.Configuration.ProxyElement class. That may have info you're looking for.

    What you describe works, you can also look in the registry.

    Here's a powershell script I wrote to check out the proxy:

    function get-proxy
    {
        $path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
        $reg = get-itemproperty $path
        return $reg
    }
    
    0 讨论(0)
  • 2020-12-01 07:14
    <system.net>
    <defaultProxy enabled="false" useDefaultCredentials="false">
      <proxy/>
      <bypasslist/>
      <module/>
    </defaultProxy>
    

    Use this snippet in application.config file.

    0 讨论(0)
  • 2020-12-01 07:15

    First, GetDefaultProxy is marked as deprecated so you have no guarantee it will be around in even the immediate future. Second, Address can be null so the code you gave risks a NullReferenceException:

    0 讨论(0)
  • 2020-12-01 07:22

    It appears that WebRequest.DefaultWebProxy is the official replacement for WebProxy.GetDefaultProxy.

    You should be able to drop that in to your original code with only a little modification. Something like:

    WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
    if (proxy.Address.AbsoluteUri != string.Empty)
    {
        Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
        wc.Proxy = proxy;
    }
    
    0 讨论(0)
  • 2020-12-01 07:22

    Setting my request proxy to WebRequest.GetSystemWebProxy() solved the problem.

    WebProxy.GetDefaultProxy() is the actual way but it is now deprecated.

    0 讨论(0)
  • 2020-12-01 07:23

    WebClient etc use the WinHTTP settings (not the IE settings), so the easiest thing to do is to configure WinHTTP! On XP etc you can use:

    proxycfg -u
    

    to import the current IE settings into the WinHTTP store. After that, WebClient etc should be able to use the same settings without issue. On Vista and Windows 7 this is now found under:

    netsh winhttp import proxy ie
    

    You need to run this as administrator.

    0 讨论(0)
提交回复
热议问题