none
How to get proxy server from automation configuration script in C# RRS feed

  • Question

  • I use 
      WebRequest.GetSystemWebProxy().GetProxy(uri) to get a proxy, 
    Will this give me the proxy when automatic configuration script is used

    How to test see if this works. I do not have an environment set up to test
    I tried putting a .pac on my local PC but it does not work.

    I also tried the following, does not work, either.
    Please help. thanks 


                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;
                }
            }

    Peace & Joy

    Tuesday, February 23, 2016 10:17 PM

All replies

  • Hi toosenstive,

    >>WebRequest.GetSystemWebProxy().GetProxy(uri) to get a proxy, 

    WebRequest.GetSystemWebProxy method will returns a proxy configured with the Internet Explorer settings of the currently impersonated user. You could set auto proxy configuration settings for Internet Explorer 11 by reference following link.
    https://technet.microsoft.com/en-us/library/dn321447.aspx

    By the way, the mime type of the PAC file would be: [Content-type: application/x-ns-proxy-autoconfig]. Other mime types might not work. For detail information, link below is for your reference.
    http://stackoverflow.com/questions/13552227/using-proxy-automatic-configuration-from-ie-settings-in-net

    Best Regards,
    Li Wang


    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.

    Thursday, February 25, 2016 2:50 AM
    Moderator
  • Thanks. 

    So how can I get the proxy server from pac file?

    my clients use automatic configuration script in their proxy setting, like https://company.depart.com/proxy.pac 

    My software tries to use the same as browser does. 

    I use GetProxyForUrlUsingPac as last post, but it does not work

    So please help

    Thanks 



    Peace & Joy

    Thursday, February 25, 2016 9:27 PM
  • I use the following code, it works for some client but not all. 

    So I wonder how to get the proxy server for a url when automatic script is used.

    Thanks

                if (_proxyForLDS == null)
                {
                    _proxyUriForLDS = WebRequest.GetSystemWebProxy().GetProxy(uri);
                    _proxyForLDS = new WebProxy(_proxyUriForLDS, true)
                    {
                        Credentials = CredentialCache.DefaultNetworkCredentials
                    };
                    if (_proxyUriForLDS != null && !String.IsNullOrEmpty(_proxyUriForLDS.AbsoluteUri) && !_proxyForLDS.Address.Equals(uri) && !RTDHelper.IsLocalHost(_proxyForLDS))
                    {
                        _realProxyForLDS = true;
                    }
                    else
                    {
                        _realProxyForLDS = false;
                    }
                }


    Peace & Joy

    Thursday, February 25, 2016 11:55 PM
  • Hi toosenstive,

    >> it works for some client but not all. 

    It is because not all the clients are configured correctly. Please make sure all the clients have do the auto proxy configuration settings for Internet Explorer 11 correctly.
    https://technet.microsoft.com/en-us/library/dn321447.aspx

    Best Regards,
    Li Wang


    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.

    Wednesday, March 2, 2016 9:34 AM
    Moderator