简体   繁体   中英

App.config Proxy configurations not working

I am creating a VSTO plugin in.Net Framework 4.6. I am using System.Net.Http.HttpClient for making all the API calls to server. Some of the clients are behind a proxy server and uses PAC file references in IE. The problem is with accessing the proxy(sometimes it works and sometimes it doesn't), I have added below code in my App.Config file after searching for suggestions on how to resolve the proxy related issues:

<system.net>
 <defaultProxy enabled = "true" useDefaultCredentials = "true">
  <proxy usesystemdefault ="True" />
 </defaultProxy>
</system.net>

With the above config added in the configuration file, it works for some clients behind proxy but do not work for others behind proxy and Vice versa with config removed(some proxy works & some don't). I tried some other options by using GetDefaultProxy() and CredentialCache.DefaultCredentials with some extra calls if the earlier calls fails. But this also do not resolve the issue.

Is there a way so that I can work with both the configurations with a single installer file? Or Any other approach I should follow to solve these kind of issues? Or Is it possible that the request is not reaching to the proxy server or firewall itself with the above System.Net configuration.

Looking for some pointers in right direction or any suggestions.

I can only offer what has worked in my past proxy issues, as opposed to info in the app.config:

CredentialCache creds = new CredentialCache();
creds.add(new Uri(proxyAddress), "Basic", new NetworkCredential(user, pass));
WebProxy proxy = new WebProxy
{
    Address = new Uri(proxyAddress),
    UseDefaultCredentials = false,
    Credentials = creds
};

HttpClientHandler handler = new HttpClientHandler
{
    UseProxy = true,
    PreAuthenticate = true,
    Proxy = proxy
};

HttpClient client = new HttpClient(handler);

This allowed me to make the HttpClient a static resource, shared throught the life of the application. Your proxy may not require all the auth that mine did but you can adjust this pretty easily.

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