Dynamically using a WebProxy with WPAD script

末鹿安然 提交于 2019-12-04 06:18:34

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!