zoukankan      html  css  js  c++  java
  • C#HTTP代理的实现之注册表实现

    HTTP代理的实现形式,可以通过修改注册表项,然后启动浏览器来实现,也可以通过SOCKET通信,构造HTTP头实现。下面是关于注册表实现的方式。


    注册表实现,只需要修改几个关键的注册表项就可以了。


    第一项:启用代理的注册表项。


    第二项:代理的IP和端口。

    第三项:连接的方式。



    第四项:让注册表项立即生效。严格来说,这一步并没有修改注册表项,而是调用API通知注册表项生效。


    下面是相关代码:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using Microsoft.Win32;
    
    namespace UtilSp.ClassLib
    {
        public class ProxySp
        {
            [DllImport("wininet.dll", SetLastError = true)]
            private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
    
            private const int INTERNET_OPTION_REFRESH = 37;
            private const int INTERNET_OPTION_PROXY = 38;
            private const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
            private const int INTERNET_OPEN_TYPE_PROXY = 3;
            private const int INTERNET_OPEN_TYPE_DIRECT = 1;
    
            #region changeUserAgent Function
            public static void changeUserAgent()
            {            
                var appName = Process.GetCurrentProcess().MainModule.ModuleName;
                RegeditSp.write(@"HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION", appName, 9999, RegistryValueKind.DWord);
            }
            #endregion
    
            #region setProxyEnabled Function
            /// <summary>
            /// Set proxy.
            /// </summary>
            /// <param name="isProxyEnabled">true:is enabled.false:is not enabled.</param>
            /// <param name="proxyIP">Proxy ip and port.Format:192.168.100.162:8080</param>
            /// <returns></returns>
            public static bool setProxy(bool isProxyEnabled, string proxyIP = "")
            {
                string regPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings";//Enabled proxy option.
                bool isSetProxyEnabledOk =
                    RegeditSp.write(RegeditSp.REGEDIT_ROOT_SET.HKEY_CURRENT_USER,
                                    regPath,
                                    "ProxyEnable",
                                    isProxyEnabled ? 1 : 0,
                                    true);
                bool isSetProxyIP = true;
                if (!string.IsNullOrEmpty(proxyIP))
                {
                    isSetProxyIP = RegeditSp.write(RegeditSp.REGEDIT_ROOT_SET.HKEY_CURRENT_USER,
                                                    regPath,
                                                    "ProxyServer",
                                                    proxyIP,
                                                    true);
                }
                bool isConnectionOK=setConnection(isProxyEnabled, proxyIP);
                bool isNotifyOk = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);//Notify proxy in the regedit has changed.Lanuch proxy when connect next.
                bool isReadOK = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);//Read proxy from regedit.
                return isSetProxyEnabledOk && isSetProxyIP && isNotifyOk && isReadOK && isConnectionOK;
            }
            #endregion
    
            #region setConnection Function
            private static bool setConnection(bool isProxyEnabled, string proxyIP)
            {
                string regPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections";//Connection register option.
                byte[] szBuf = new byte[80];
                szBuf[0] = 0x3c;
                szBuf[4] = 0x09;
                szBuf[8] = (byte)(isProxyEnabled ? 0x03 : 0x01);
    
                szBuf[12] = (byte)proxyIP.Length;
                for (int i = 0; i < proxyIP.Length; i++)
                {
                    szBuf[i + 16] =(byte)Convert.ToInt32(proxyIP[i]);
                }            
                string local = "<local>";
                for (int j = 0; j < 7; j++)
                {
                    szBuf[20 + proxyIP.Length + j] = (byte)Convert.ToInt32(local[j]);
                }
                
                return  RegeditSp.write(RegeditSp.REGEDIT_ROOT_SET.HKEY_CURRENT_USER,
                                    regPath,
                                    "宽带连接",
                                    szBuf,
                                    true);
            }
            #endregion
        }
    }
    


    附上工程代码 http://download.csdn.net/detail/xxdddail/5831359。工程中实现了自动用代理IP列表刷网页的功能。


  • 相关阅读:
    java并发系列(六)-----Java并发:volatile关键字解析(内存语义、实现原理)
    java并发系列(五)-----如何正确的关闭一个线程
    23.备忘录模式(Memento Pattern)
    22.访问者模式(Vistor Pattern)
    21.责任链模式
    20.策略者模式(Stragety Pattern)
    19.状态者模式(State Pattern)
    18.中介者模式(Mediator Pattern)
    17.观察者模式(Observer Pattern)
    16.迭代器模式(Iterator Pattern)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3223541.html
Copyright © 2011-2022 走看看