none
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:

    http://www.codeproject.com/Articles/12168/Using-PAC-files-proxy 

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

    Regards,


    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:
    https://marckean.wordpress.com/2010/02/09/setting-up-proxy-pac-files-in-iis7-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
    Moderator
  • 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
            {
                get
                {
                    return WebRequest.GetSystemWebProxy();
                }
            }

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

    Regards,


    Chetan Rajakumar

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

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

    http://www.codeproject.com/Articles/12168/Using-PAC-files-proxy  

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

         IntPtr WinHttpSession = Win32Api.WinHttpOpen("User",
                                        Win32Api.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                        IntPtr.Zero,
                                        IntPtr.Zero,
                                        0);

         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 |
                                           Win32Api.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
         ProxyOptions.lpszAutoConfigUrl = PacUri;
               
         // Get Proxy
         bool IsSuccess = Win32Api.WinHttpGetProxyForUrl( WinHttpSession,
                                                          DestinationUrl,
                                                          ref ProxyOptions,
                                                          ref ProxyInfo );
               
         Win32Api.WinHttpCloseHandle(WinHttpSession);

         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:

    http://www.whatismyproxy.com/

    This web page will display information about your proxy server.

    Tuesday, May 5, 2015 10:32 AM