6

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

Although IE has the correct proxysettings, the application is running as a windows service running under Local System account, so the application does not know the IE-settings for this windows-user.

Putting the following in app.config works:

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

But this has the restriction that it cannot be configured by the user. Is there a way to do the above dynamically from (C#-)code? I also suspect the above will change behaviour of webservices that should not go through a proxy (but I have not verified that).

At http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx I found the helpful text: "(For an example that demonstrates using the WPAD feature, see the documentation for the IWebProxyScript class.)" but I have not found the example :(.

2 Answers2

1

This article on code project shows how to use windows APIs to execute the PAC script and return the correct proxy details for a given url: http://www.codeproject.com/Articles/12168/Using-PAC-files-proxy

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

sga101
  • 1,894
  • 12
  • 12
  • I found something similar here: [link](http://www.pcreview.co.uk/forums/winhttpgetproxyforurl-t1862181.html) but WPADs can return several proxyservers (fail over and/or load balancing, see [link](http://en.wikipedia.org/wiki/Proxy_auto-config)). It then returns them semicolon separated. Entries returning "DIRECT" seem to disappear using this method. So although this is a step in the right direction, it does not seem complete to me. – Jan-Jaap van der Geer Jan 25 '12 at 14:12
  • Could you not use string.split on the return value to get a list of entries, and then try them in sequence, defaulting to not using a proxy if the list is empty? – sga101 Jan 25 '12 at 15:58
  • That is what I ended up doing. But I do not like it: - I now have a loop doing the webrequest for each proxy I find - I don't know how to handle DIRECT. I now use no proxy if I get an empty list, but I've seen examples returning both a proxy and DIRECT, so that is probably not really the correct way of doing it. - If the webrequest is timing out, I don't really know why. Could be the proxy, could be something else. So I repeat the request with another proxy. Maybe another timeout? And the user waits... - I really don't want to do this. I want the OS to handle it... – Jan-Jaap van der Geer Jan 25 '12 at 17:38
  • This is the way IE works when using auto configuration scripts. If this introduces a problem, it is either with the PAC script (beyond your application's control) or with the proxy server (ditto), so you can't really do any better. Granted, if your web service is down, the user will have to wait for you to check all the proxies before they find out. DIRECT simply means connect directly - do not use a proxy. – sga101 Jan 25 '12 at 18:16
0

IWebProxyScript is used internally by WebProxy itself.

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

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

You can also get out the proxy URL for a given target like so:

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

This DOES NOT work for PAC scripts.

friggle
  • 3,065
  • 1
  • 33
  • 46