【轉】使用程序修改系統(IE)代理設置


文章都是發布在github再轉到這邊的,這邊格式可能會亂掉.博客地址:benqy.com

 

這是本人在做的一個前端開發調試工具(HttpMock),功能是web服務器+http日記+http代理(類似fiddler),其中的代理功能,需要在web服務啟動時,自動去設置各瀏覽器的代理設置.

我原先是通過寫注冊表的方式去實現,實現方法很簡單,寫一個注冊表文件,啟動的時候自動運行一下這個注冊表文件就可以.

修改代理的注冊表文件的內容,proxy.reg:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]  
"MigrateProxy"=dword:00000001  
"ProxyEnable"=dword:00000001  
"ProxyServer"="http=127.0.0.1:17173"

類似的,取消代理的注冊表文件,disproxy.reg:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000000
"ProxyServer"=""

這個方式非常的簡單,但是存在一個嚴重的問題,就是修改完注冊表之后,如果不重啟ie,代理設置不會馬上生效(這樣做出來的軟件,實在太山寨,簡直沒法用啊,什么爛軟件).

后來,發現fiddler是用wininet這個工具來設置ie代理.因此,google了一番之后,找到了wininet.dll這個東西,然后用.NET寫了個簡單的命令行工具.功能很簡單,直接上代碼:

ProxyManager.cs

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace proxysetting
{
    public class ProxyManager
    {
        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lPBuffer, int lpdwBufferLength);
        private const int INTERNET_OPTION_REFRESH = 0x000025;
        private const int INTERNET_OPTION_SETTINGS_CHANGED = 0x000027;
        private const string regeditKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
        private List<string> proxyLibs = new List<string>();

        private void Reflush()
        {
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }

        public void Add(string server)
        {
            this.proxyLibs.Add(server);
        }

        public void Run()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(regeditKey, true);
            key.SetValue("ProxyServer", String.Join(";", this.proxyLibs.ToArray()));
            key.SetValue("ProxyEnable", 1);
            key.Close();
            this.Reflush();
        }

        public void Stop()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(regeditKey, true);
            key.SetValue("ProxyEnable", 0);
            key.Close();
            this.Reflush();
        }
    }
}

ProxyManager類簡單的封裝wininet.dll的代理設置接口,和使用注冊表文件的區別在於this.Reflush()這個方法,強制刷新代理設置,這樣就不用重啟IE了.

然后是在Main函數里調用:

Program.cs

namespace proxysetting
{
    class Program
    {
        static void Main(string[] args)
        {

            var pm = new ProxyManager();
            if (args.Length < 1) return;
            if (args[0] == "stop")
            {
                pm.Stop();
            }
            else
            {
                for (var i = 0; i < args.Length; i++)
                {
                    pm.Add(args[i]);
                }
                pm.Run();
            }
        }
    }
}

通過命令行參數來設置代理,可以用空格來設置多個協議的代理,如下:

設置http和https代理,代理ip為127.0.0.1,端口號17173
proxysetting http=127.0.0.1:17173 https=127.0.0.1:17173
nodejs中調用:

exports.setProxy = function () {
  var exec = require("child_process").exec;
  exec(__dirname + 'proxysetting http=127.0.0.1:17173 https=127.0.0.1:17173');
};

 

取消代理 
proxysetting stop
nodejs中調用:

exports.disProxy = function () {
  var exec = require("child_process").exec;
  exec(__dirname + 'proxysetting stop');
};

 

說了那么多,其實就一句話:wininet的this.Reflush方法.

from:http://www.cnblogs.com/honghongming/archive/2014/01/24/3532567.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM