简体   繁体   中英

Programmatically changing defaultproxy instead of using app.config

I'm creating a desktop application in WPF. This application uses webclient instances to communicate with an API to collect data from.

In this desktop application I want to create a checkbox which should allow the user to ignore the internet options proxy or to use the default auto detect options.

At this moment I added this defaultproxy setting to my app.config to stop my application from trying to communicate through the proxy and instead ignore it.

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

When I don't have this code in my app.config, my application will try to use the default internet options proxy.

So in order to have this switchable by a checkbox in a settings menu I will have to change these settings programatically.

I am aware of the fact that when I set the default proxy to a new webproxy that the application will ignore the internetoptions proxy.

WebRequest.DefaultWebProxy = new WebProxy();

But I can't for the life of me figure out how I can set this back to the automatically detecting of proxy use, like before I inserted above defaultproxy settings in the app.config.

I am testing this by using a faulty proxy. This means that if I send an api request, the proxy can't be found and I receive a webexception. When I ignore the proxy with the app.config code, the request uses my normal internet connection, and returns API data.

It would really help me out if anyone could tell me how I can programatically set my application to ignore the proxy or, most importantly, to use the default auto detection settings.

If you want to set the proxy back to the default proxy, you can use this static method

WebRequest.GetSystemWebProxy();

see here https://msdn.microsoft.com/de-de/library/system.net.webrequest.getsystemwebproxy(v=vs.110).aspx

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