C#HTTP代理的實現之註冊表實現

來源:互聯網
上載者:User

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_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_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列表刷網頁的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.