HTTP代理的实现形式,可以通过修改注册表项,然后启动浏览器来实现,也可以通过SOCKET通信,构造HTTP头实现。下面是关于注册表实现的方式。
注册表实现,只需要修改几个关键的注册表项就可以了。
第一项:启用代理的注册表项。
第二项:代理的IP和端口。
第四项:让注册表项立即生效。严格来说,这一步并没有修改注册表项,而是调用API通知注册表项生效。
下面是相关代码:
注册表实现,只需要修改几个关键的注册表项就可以了。
第一项:启用代理的注册表项。
第二项:代理的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
}
}