C# 修改浏览器代理地址

参考地址 https://www.cnblogs.com/skybreak/p/3554229.html

 

调用方式为 :

IEProxy.SetProxy(strProxy);

可用的免费代理网站: 

http://www.xicidaili.com

http://www.ip3366.net

http://www.66ip.cn

 下方为修改代理的类:



namespace CatchDataFrame.Utils
{
    public class IEProxy
    {
        public static bool UnsetProxy()
        {
            return SetProxy(null, null);
        }

        public static bool SetProxy(string strProxy)
        {
            return SetProxy(strProxy, null);
        }

        public static bool SetProxy(string strProxy, string exceptions)
        {
            InternetPerConnOptionList list = new InternetPerConnOptionList();

            int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
            InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
            //use  a proxy server
            options[0].M_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
            options[0].M_Value.M_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT :
                (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
            //use this proxy server
            if(optionCount>1)
            {
                options[1].M_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVE;
                options[1].M_Value.M_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
                //except for these addresses
                if(optionCount>2)
                {
                    options[2].M_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
                    options[2].M_Value.M_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
                }
            }

            //default stuff
            list.DwSize = Marshal.SizeOf(list);
            list.SzConnection = IntPtr.Zero;
            list.DwOptionCount = options.Length;
            list.DwOptionError = 0;

            int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
            //make a pointer out of all that
            IntPtr optionPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
            //copy the array  over into that  spot in memory
            for(int i=0;i
    /// options manifests for Internet{Query|Set} Option
    /// 
    public enum InternetOption : uint
    {
        INTERNET_OPTION_PER_CONNECTION_OPTION = 75
    }

    /// 
    /// Options used in INTERNET_PER_CONN_OPTION struct
    /// 
    public enum PerConnOption
    {
        /// 
        /// Sets or retrieves the connection type.The vlaue member will contain one or more of the values from PerConnFlags
        /// 
        INTERNET_PER_CONN_FLAGS = 1,
        /// 
        /// Sets or retrieves a string containing the proxy servers 
        /// 
        INTERNET_PER_CONN_PROXY_SERVE = 2,
        /// 
        /// Sets or retrieves a string containing the urls what do not  user the proxy server 
        /// 
        INTERNET_PER_CONN_PROXY_BYPASS = 3,
        /// 
        /// Sets or retrieves a string containing the url to the automatic configuration script
        /// 
        INTERNET_PER_CONN_AUTOCONFIG_URL = 4
    }

    /// 
    /// PER_CONN_FLAGS
    /// 
    [Flags]
    public enum PerConnFlags
    {
        PROXY_TYPE_DIRECT = 0x00000001, //direct to net 
        PROXY_TYPE_PROXY = 0x00000002, //via named proxy
        PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, //autoproxy url
        PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection
    }

    #endregion

    internal static class NativeMethods
    {
        [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
    }

}

 

查验结果:

可通过各浏览器的设置-->代理-->连接-->局域网设置。查看修改后的代理地址。

C# 修改浏览器代理地址_第1张图片

你可能感兴趣的:(c#爬取)