添加引用CertMaker.dll,BCMakeCert.dll,FiddlerCore4.dll
启动程序时 设置Fiddler代理127.0.0.1端口为8877,程序关闭时或不使用时关闭Fiddler代理,并清空代理设置;
第一次使用需安装Fiddler证书 InstallCertificate();
//启动fiddler设置代理,安装证书,此处SetIEProxy改变的为全局代理设置,若无需改变全局代理设置,可进行局部代理设置。 Fiddler.FiddlerApplication.BeforeRequest += FiddlerApplicaiton_BeforeRequest; Fiddler.FiddlerApplication.BeforeResponse += FiddlerApplicaiton_BeforeResponse; Fiddler.FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete; Fiddler.FiddlerApplication.Startup(8877, false, true, true); SetIEProxy(); InstallCertificate(); //关闭fiddler Fiddler.FiddlerApplication.Shutdown();
//安装证书-安装完毕后删除CertMaker.dll,可避免重复安装 public static bool InstallCertificate() { if (!CertMaker.rootCertExists()) { if (!CertMaker.createRootCert()) return false; if (!CertMaker.trustRootCert()) return false; } CONFIG.IgnoreServerCertErrors = true; return true; } //卸载证书,通常不用 public static bool UninstallCertificate() { if (CertMaker.rootCertExists()) { if (!CertMaker.removeFiddlerGeneratedCerts(true)) return false; } return true; } //请求前 public void FiddlerApplicaiton_BeforeRequest(Session oS) { oS.bBufferResponse = true; } //请求中 public void FiddlerApplicaiton_BeforeResponse(Session oS) { //解压 oS.utilDecodeResponse(); } //请求结束 public void FiddlerApplication_AfterSessionComplete(Session oS) { }
//设置IE代理 public bool SetIEProxy() { try { Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); //设置代理可用 rk.SetValue("ProxyEnable", 1); //设置代理IP和端口 rk.SetValue("ProxyServer", "http=127.0.0.1:8877;https=127.0.0.1:8877"); rk.Flush();//刷新注册表 rk.Close(); ActivateIEProxy(); return true; } catch { return false; } } //清空IE代理设置 public bool SetCleanIEProxy() { try { Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); rk.SetValue("ProxyEnable", 0); rk.Flush();//刷新注册表 rk.Close(); ActivateIEProxy(); return true; } catch { return false; } } //激活IE设置 public void ActivateIEProxy() { InternetSetOption(0, 39, IntPtr.Zero, 0); InternetSetOption(0, 37, IntPtr.Zero, 0); }
评论前必须登录!
注册