Fidder插件自动生成爬虫代码(C#)

 原创,效果如下:

1、新建项目,并添加Fidder.exe的引用:

 2、添加代码

[assembly: Fiddler.RequiredVersion("2.2.8.6")]
using
Fiddler; using System; using System.Windows.Forms; public class MyExtension : IHandleExecAction, IFiddlerExtension { private TabPage tabPage; //创建插件的选项卡页 private UserControl myCtrl; //UserControl public MyExtension() { #region makeUC var uc = new System.Windows.Forms.UserControl(); var txtCode = new System.Windows.Forms.RichTextBox(); txtCode.Dock = System.Windows.Forms.DockStyle.Fill; txtCode.Location = new System.Drawing.Point(0, 0); txtCode.Name = "txtCode"; txtCode.Size = new System.Drawing.Size(150, 150); uc.Controls.Add(txtCode); #endregion //构造函数中实例化对象 this.myCtrl = uc; this.myCtrl.Dock = DockStyle.Fill; this.tabPage = new TabPage("Coder");//选项卡的名字为Test } public void OnLoad() { //将用户控件添加到选项卡中 this.tabPage.Controls.Add(this.myCtrl); //为选项卡添加icon图标,这里使用Fiddler 自带的 this.tabPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline; //将tabTage选项卡添加到Fidder UI的Tab 页集合中 FiddlerApplication.UI.tabsViews.TabPages.Add(this.tabPage); FiddlerApplication.UI.lvSessions.SelectedIndexChanged += LvSessions_SelectedIndexChanged; } public void OnBeforeUnload() { } private void LvSessions_SelectedIndexChanged(object sender, EventArgs e) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); var sessions = FiddlerApplication.UI.GetSelectedSessions(); if (sessions != null) { if (sessions.Length > 0) { sb.Append(" HttpHelper httpHelper = new HttpHelper();\r\n"); } foreach (var item in sessions) { sb.Append(Raw2Code(item.RequestHeaders.ToString(true, true, true))); } } myCtrl.Controls[0].Text = sb.ToString(); } private string Raw2Code(string rawText) { return "请加QQ:351392535;www.shujucaiji.cn"; } bool IHandleExecAction.OnExecAction(string sCommand) { string[] strArray = Utilities.Parameterize(sCommand); var aa = FiddlerApplication.UI.GetAllSessions(); return true; } //public void AutoTamperRequestBefore(Session oSession) //{ // /*在这编写请求之前需要执行的code */ //} //public void AutoTamperRequestAfter(Session oSession) //{ // /*在这编写请求之后需要执行的code */ //} //public void AutoTamperResponseBefore(Session oSession) //{ // /*在这编写响应之前需要执行的code */ // myCtrl.txtCode.Text = Raw2Code(oSession.RequestHeaders.ToString(true, true, true)); //} //public void AutoTamperResponseAfter(Session oSession) //{ // /*在这编写响应之后需要执行的code */ //} //public void OnBeforeReturningError(Session oSession) //{ // /*在这编写有错误返回时需要执行的code */ //} }

 3、编译并将dll拷贝到Fidder的Script目录下。

       或者在:后期生成事件命令行中添加:copy "$(TargetPath)" "%ProgramFiles%\Fiddler2\Scripts\$(TargetFilename)"

4、打开Fidder

原文地址:https://www.cnblogs.com/ssda/p/9757307.html