C# 自动检测代理设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/844467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:52:56  来源:igfitidea点击:

C# auto detect proxy settings

c#networkingproxy

提问by ant2009

C# 2008 SP1

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.

我正在使用代码来检测是否在“Internet 选项”下设置了代理。如果有代理,那么我将在我的网络客户端中设置它。

So I am just checking if the address of the proxy exists. If there is not, then there is no proxy to set in the webclient.

所以我只是检查代理的地址是否存在。如果没有,则在 webclient 中没有要设置的代理。

Is this the correct way to do this:

这是执行此操作的正确方法吗:

Many thanks for any advice,

非常感谢您的任何建议,

WebProxy proxy = WebProxy.GetDefaultProxy();

if (proxy.Address.ToString() != string.Empty)
{
    Console.WriteLine("Proxy URL: " + proxy.Address.ToString());
    wc.Proxy = proxy;
}

====== Code edit ======

====== 代码编辑 ======

[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved);

[Flags]
enum InternetConnectionState_e : int
{
    INTERNET_CONNECTION_MODEM = 0x1,
    INTERNET_CONNECTION_LAN = 0x2,
    INTERNET_CONNECTION_PROXY = 0x4,
    INTERNET_RAS_INSTALLED = 0x10,
    INTERNET_CONNECTION_OFFLINE = 0x20,
    INTERNET_CONNECTION_CONFIGURED = 0x40
}     

// Return true or false if connecting through a proxy server
public bool connectingThroughProxy()
{
    InternetConnectionState_e flags = 0;
    InternetGetConnectedState(ref flags, 0);
    bool hasProxy = false;

    if ((flags & InternetConnectionState_e.INTERNET_CONNECTION_PROXY) != 0)
    {
        hasProxy = true;
    }
    else
    {
        hasProxy = false;
    }

    return hasProxy;
}

采纳答案by Nathan Stohlmann

It appears that WebRequest.DefaultWebProxyis the official replacementfor WebProxy.GetDefaultProxy.

看来WebRequest.DefaultWebProxy是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;
}

回答by John Weldon

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

查看 System.Net.Configuration.ProxyElement 类。这可能有您正在寻找的信息。

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

您描述的内容有效,您也可以在注册表中查看。

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

这是我编写的用于检查代理的 powershell 脚本:

function get-proxy
{
    $path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    $reg = get-itemproperty $path
    return $reg
}

回答by Matthew Flaschen

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

首先,GetDefaultProxy被标记为已弃用,因此您无法保证即使在不久的将来它也会存在。其次,地址可以为空,因此您提供的代码可能会引发 NullReferenceException:

回答by Marc Gravell

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

WebClient等使用 WinHTTP 设置(不是 IE 设置),所以最简单的方法是配置 WinHTTP!在 XP 等上,您可以使用:

proxycfg -u

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

将当前的 IE 设置导入 WinHTTP 商店。之后,WebClient等应该能够毫无问题地使用相同的设置。在 Vista 和 Windows 7 上,现在可以在以下位置找到:

netsh winhttp import proxy ie

You need to run this as administrator.

您需要以管理员身份运行它。

回答by EdsonF

Try the following:

请尝试以下操作:

public string GetMeMyInfo(string searchCriteria)
{
    // Instatiate the web service and declare the necessary variables
    WsService.WsServiceBus oWsGetInfo = new WsService.WsServiceBus();
    // Configure the Web Service Proxy
    oWsGetInfo.Proxy = System.Net.WebProxy.GetDefaultProxy();
    oWsGetInfo.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    // Invoke the web service
    return oWsGetInfo.GetInfo4Me(searchCriteria);
}

That will get the default proxy setting and credentials before invoking your web service for example.

例如,这将在调用您的 Web 服务之前获得默认代理设置和凭据。

回答by iCoder

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

设置我的请求代理来WebRequest.GetSystemWebProxy()解决问题。

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

WebProxy.GetDefaultProxy()是实际的方式,但现在已弃用。

回答by Manoj

<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
  <proxy/>
  <bypasslist/>
  <module/>
</defaultProxy>

Use this snippet in application.config file.

在 application.config 文件中使用此代码段。

回答by SpeedOfSpin

This works for me

这对我有用

        var proxy = WebRequest.GetSystemWebProxy();
        Uri testUrl = new Uri("http://proxy.example.com");
        var proxyUrl = proxy.GetProxy(testUrl);
        if (proxyUrl != testUrl)
            //Use your proxy here
        else
            //We are not using a proxy