Dynamic use of WebProxy with WPAD script

I am trying to call a web service. I need to use a proxy server that uses a WPAD script. The URL of this WPAD script is different for different application deployments.

Although IE has the correct proxy servers, the application runs as a Windows service running under the Local System account, so the application does not know the IE settings for this Windows user.

The following has been added to app.config:

<system.net> <defaultProxy enabled="true" useDefaultCredentials="true" > <proxy autoDetect="True" scriptLocation="http://url.to/wpad.dat"/> </defaultProxy> </system.net> 

But this has a limitation that the user cannot configure. Is there a way to do this above dynamically from (C # -) code? I also suspect that the above will change the behavior of web services that should not go through a proxy server (but I did not confirm this).

At http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx I found a useful text: "(An example demonstrating the use of the WPAD function, see the documentation for the IWebProxyScript class.)", But I did not find an example: (.

+6
source share
2 answers

This code draft article shows how to use the window API to execute a PAC script and return the correct proxy data for a given URL: http://www.codeproject.com/Articles/12168/Using-PAC-files-proxy

You can use this function to find out the details of the proxy server, and then directly configure the proxy server of the web service objects or change WebRequest.DefaultProxy.

+1
source

IWebProxyScript is used internally by WebProxy itself.

If you initialize WebProxy with the URL of the WPAD script, it will resolve the correct URL for requests sent to it. You can set this WebProxy to WebRequest, and it will automatically handle setting the correct proxy URL for the request target.

 WebRequest request = WebRequest.Create("http://targeturl"); request.Proxy = new WebProxy("http://url.to/wpad.dat"); 

You can also get the proxy server url for a given purpose, for example:

 WebProxy proxy = new WebProxy("http://url.to/wpad.dat"); Uri proxyUri = proxy.GetProxy(new Uri("http://targeturl")); 

This does NOT work for PAC scripts.

0
source

Source: https://habr.com/ru/post/906203/


All Articles