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列表刷網頁的功能。