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列表刷网页的功能。

C#HTTP代理的实现之注册表实现的更多相关文章

  1. Docker 开源项目之 registry - 部署 registry (注册表)服务器

    原文地址 在部署 registry 之前需要现在主机上安装 Docker.registry 实际上就是运行在 Docker 中的 registry 镜像的实例. 本主题提供关于部署和配置 regist ...

  2. Win.ini和注册表的读取写入

    最近在做打包的工作,应用程序的配置信息可以放在注册表文件中,但是在以前的16位操作系统下,配置信息放在Win.ini文件中.下面介绍一下Win.ini文件的读写方法和注册表的编程. 先介绍下Win.i ...

  3. 卸载oracle之后,如何清除注册表

    之前卸载了oracle,今天偶然间发现,在服务和应用程序里面,还残存着之前的oracle服务.原来,还需要去清理下注册表. 在开始菜单的这个框里面 输入regedit,进入注册表.找到这个目录 HKE ...

  4. 利用注册表在右键添加VS15的快捷方式打开文件夹

    1.简介 最近安装VS15 Preview 5,本版本可以打开"文件夹" 是否可以向Visual Studio Code一样在文件夹或文件右键菜单添加"Open with ...

  5. 修改策略组/注册表 屏蔽Win10升级解决方法

    一.Windows非家庭版 第1步:按Win+R键调出运行对话框,输入命令“gpedit.msc”,按回车键启动组策略编辑器. 第2步:依次定位到“计算机配置→管理模板→Windows组件→Windo ...

  6. Windows 7安装软件时无法将注册值写入注册表的处理方法

    1. 我们来确认一下,有没有安装什么软件把注册表给封了.如杀毒软件,防火墙等.把这些软件关了之后,再安装软件试试:如果不行,就把杀毒软件卸载了,再安装软件试试. 2. 更改组策略设置 步骤: 开始-运 ...

  7. MFC操作注册表

    1.创建和修改注册表 BOOL CTestToolCtr::GetHkey(CString strHkey, HKEY& hkey) { == strHkey.CompareNoCase(_T ...

  8. Win 通过修改注册表把CapsLock映射为Rshift

    成品: REGEDIT4     [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout] "Scancod ...

  9. js通过注册表找到本地软件安装路径并且执行

    场景:用js执行本地的安装软件,如果不存在就执行安装 操作步骤: 1.前台js代码 <script type="text/javascript"> function e ...

随机推荐

  1. crawler_http关闭连接

    1:ps aux|grep Spider4Test.jar 查看端口 2: lsof  -p [端口号] 在爬虫运行期间如果看到 大量的 TIME_WAIT  WAIT_CLOSE 说明请求关闭阻塞[ ...

  2. oracle_根据ID(字符型)建立分区表

    方案思路:有一张暴增的数据表(10亿级别),以后需求需要提高单条查询性能, 这个表有个唯一ID, 假设是UUID,采用区分首字母的方法,不同字母的数据入到不同的物理文件中. 第一步: 查找数据库服务器 ...

  3. php_公共方法01_传入数组_打印可见信息

    function decodeUnicode($str) { return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'convert', $str ...

  4. css布局之选择切换按钮

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Samza/KafkaAnalysizing

    Apache Samza is a distributed stream processing framework. It uses Apache Kafka for messaging, and A ...

  6. ZOJ 2724 Windows 消息队列 (优先队列)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fund ...

  7. Java初认识--Java语言的书写规范及基本的运算符

    一.Java中名称的规范和书写程序的规范. 1.Java中的名称规范: (1)包名全是小写:xxyyzz: (2)类名接口名:首字母大写:XxxYyy: (3)变量名和函数名:变量名不能是关键字:多单 ...

  8. IOS程序启动的过程

    IOS程序启动按照以下5个步骤执行 1.main函数 IOS程序启动首先执行main函数 2.UIApplicationMain 执行main函数中的UIApplicationMain函数,这个函数会 ...

  9. Mac下Android配置及unity3d的导出Android

    昨晚实在弄的太晚了,费尽脑汁才弄出来. ok,关于mac下的eclipse的安卓配置,我仅仅贴一个网址,就ok了 http://developer.android.com/sdk/index.html ...

  10. s性能优化方面的小知识

    总结的js性能优化方面的小知识 前言 一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够 ...