简体   繁体   中英

how to handle proxy properly in .NET

I have an Excel AddIn written in C#, which connects to server to get data via httpwebrequest

One client has proxy settings "Use Automatic Configuration Script" checked and it uses some script there.

My addin is not able to connect to server in this case.

So I open fiddler to check why it fails. Then my addin starts working.

I checked proxy settings with fiddler open, look, it is changed to "Use a proxy server for your LAN"

I want to do the same thing in my code, use the proxy setting from IE settings and use it in my code.

Do you know how to accomplish this?

What I have now is as below and does NOT work. Thanks

private static void SetProxyIfNeeded(HttpWebRequest request, Uri uri)
{
    var stopWatch = new Stopwatch();
    stopWatch.Start();            
    if (_proxy == null)
    {

        _proxyUri = WebRequest.GetSystemWebProxy().GetProxy(uri);
        _proxy = new WebProxy(_proxyUri, true)
                        {
                            Credentials = CredentialCache.DefaultNetworkCredentials
                        };
        if (_proxyUri != null && !string.IsNullOrEmpty(_proxyUri.AbsoluteUri) && !_proxy.Address.Equals(uri) && !IsLocalHost(_proxy))
        {
            _realProxy = true;
        }
        else
        {
            _realProxy = false;
        }
    }
    //if there is no proxy, proxy will return the same uri
    //do we need check if client.Proxy is null or not,
    if (_realProxy)
    {
        request.Proxy = _proxy;
    }
    stopWatch.Stop();
    Helper.LogError("\r\n Got proxy in " + stopWatch.ElapsedMilliseconds + "ms.\r\n");
}

In addition, I have a config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4netConfig.xml" />
  </appSettings>
  <system.net>
    <defaultProxy enabled ="true" useDefaultCredentials = "true">
      <proxy usesystemdefault ="True" bypassonlocal="True"/>
    </defaultProxy>
  </system.net>
</configuration>

Edit: client updates from their IT guy, looks like pac is downloaded but it is not used I do not know why it is not used, I specify to use it in each request except cometd which has no way to specify proxy, maybe that's the issue?

If using an app.config for your AddIn is an option (I know this is tricky with Office Addins) you could handle all the proxy configuration there:

<configuration>
    <system.net>
        <defaultProxy>
            <proxy
                usesystemdefault="true"
                bypassonlocal="true"
            />
        </defaultProxy>
    </system.net>
</configuration>

See <defaultProxy> Element (Network Settings) on MSDN for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM