Click here to Skip to main content
13,004,421 members (73,303 online)
Click here to Skip to main content

Stats

94.9K views
2.7K downloads
43 bookmarked
Posted 4 Nov 2005

Using PAC files proxy

, 4 Nov 2005
This article explains how to set a proxy using PAC files.
using System;

namespace PacProxyUsage
{
	/// <summary>
	/// Summary description for Proxy.
	/// </summary>
	public class Proxy
	{
		public Proxy()
		{
		}

        /// <summary>
        /// Return proxy for requested Url
        /// </summary>
        /// <param name="sUrl">url</param>
        /// <param name="sPacUri">Uri to PAC file</param>
        /// <returns>proxy info</returns>
        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;
            }
        }
       
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Glaxalg
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by Glaxalg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid