Set the Proxy by reading PAC file RRS feed

  • Question

  • Hi,

    I want to Set the Proxy for client request by reading the PAC file that is hosted in some server.

    For example,

    If the proxy type that my application should use is PACConfigured, i have to read the PAC file and set the Proxy object.

    case PacConfigured:
                        WebProxy proxy = new WebProxy(PacURL);
                        proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                        proxy.UseDefaultCredentials = true;
                        return proxy;

    PacUrl is nothing but the url of the pac file where it is hosted. Eg: http://MyServerAddress/proxy.pac

    I tried with above code but i am getting Error: "Could not connect to the remote server. Please ensure your computer is online and that you can connect to internet."

    I doubt whether i am writing a correct code to serve the requirement. I even tried this link: 

    But it also did not work. Please help me to achieve this requirement. Thanks in advance.


    Chetan Rajakumar

    Thursday, April 23, 2015 11:35 AM

All replies

  • Hi Chetan Rajakumar,

    >>"Could not connect to the remote server. Please ensure your computer is online and that you can connect to internet."

    First please try to make sure that your PAC file URL is correct. Then are you hosting the PAC file in the IIS? If so, please try to make sure that you have hosted the PAC file very well in the IIS server, for more information, please try to refer to the following article:
    #How to set up Proxy .pac files in IIS for proxy use: .

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 24, 2015 5:21 AM
  • Hi Amy,

    Thanks for the reply.

    I am hosting the pac file in IIS, configuring pac file is absolutely correct at my end because i have verify this by configuring IE LAN setting to use pac file and by entering correct and wrong proxy in pac file , it works as expected.

    But In my code, i think i am not setting the 'Proxy' object correctly to use pac file.I think i have to set the 'ScriptEngine' of Webproxy to 'AutoWebProxyScriptengine' and inside this i have to set the value of  'AutomaticConfigurationScript' as pac file url(http://MyServerAddress/proxy.pac).

    For testing purpose : If i configure the IE LAN setting to use pac file and In my code if i set the 'Proxy' object as SystemSettings as shown in below code, the 'Proxy' object will contain ScriptEngine and 'AutomaticConfigurationScript' with the value i have entered in Intenet options.

    public static IWebProxy SystemWebProxy
                    return WebRequest.GetSystemWebProxy();

    Please let me know how can set the Proxy object to use pac file.


    Chetan Rajakumar

    Friday, April 24, 2015 5:51 AM
  • Hi Amy,

    I tried below method to get the proxy details from the pac file,  

    public static string GetProxyForUrlUsingPac ( string DestinationUrl, string PacUri ){

         IntPtr WinHttpSession = Win32Api.WinHttpOpen("User",

         Win32Api.WINHTTP_AUTOPROXY_OPTIONS  ProxyOptions =
                  new Win32Api.WINHTTP_AUTOPROXY_OPTIONS();
         Win32Api.WINHTTP_PROXY_INFO ProxyInfo =
                         new Win32Api.WINHTTP_PROXY_INFO();
         ProxyOptions.dwFlags           = Win32Api.WINHTTP_AUTOPROXY_CONFIG_URL;
         ProxyOptions.dwAutoDetectFlags = (Win32Api.WINHTTP_AUTO_DETECT_TYPE_DHCP |
         ProxyOptions.lpszAutoConfigUrl = PacUri;
         // Get Proxy
         bool IsSuccess = Win32Api.WinHttpGetProxyForUrl( WinHttpSession,
                                                          ref ProxyOptions,
                                                          ref ProxyInfo );

         if ( IsSuccess ){
             return ProxyInfo.lpszProxy;
         }else {
             Console.WriteLine("Error: {0}", Win32Api.GetLastError() );
             return null;

    I get 'IsSuccess' as false and it writes Error: 0 without displaying the correct error message to know why it as failed.

    Please let me know if there is any other method to parse the pac file and get the proxy details.Thanks in advance.

    Chetan Rajakumar

    Monday, April 27, 2015 5:55 AM
  • To find your Web Proxy, simply go to:

    This web page will display information about your proxy server.

    Tuesday, May 5, 2015 10:32 AM