Click here to Skip to main content
11,581,852 members (71,753 online)
Click here to Skip to main content
Add your own
alternative version

Using PAC files proxy

, 4 Nov 2005 78.4K 2.1K 40
This article explains how to set a proxy using PAC files.
pacproxyusage.zip
PacProxyUsage.exe
pacproxyusage_src.zip
Proxy.pac
PacProxyUsage.csproj.user
App.ico
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...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by Glaxalg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid