FiddlerCore拦截本地请求初学习

首先从官网下载FiddlerCoreAPI
https://www.telerik.com/purchase/fiddlercore

查阅手册,看几个关键的方法和变量:

//获得Request体

oSession.GetRequestBodyAsString()      

//获得Response内容

oSession.GetResponseBodyAsString()

 // 修改session中的显示样式

 oSession["ui-color"] = "orange";

 // 移除http头部中的MQB-X5-Referer字段

 oSession.oRequest.headers.Remove("MQB-X5-Referer");

 // 修改http头部中的Cache-Control字段

 oSession.oRequest["Cache-Control"] = "no-cache";

 // 修改host

 oSession.host = "example.domain"; 

 // 修改Origin字段

 oSession.oRequest["Origin"] = "http://domain";

 // 删除所有的cookie

 oSession.oRequest.headers.Remove("Cookie");

 // 新建cookie

 oSession.oRequest.headers.Add("Cookie", "username=cookiename;");

 // 修改Referer字段

 oSession.oRequest["Referer"] = "https://yoururl";

 拦截websockets请求

 FiddlerApplication_OnWebSocketMessage  

  public partial class Form1 : Form
    {
        public int port = 8888;
        public Form1()
        {
          //  Iptext = new TextBox();
          //  Iptext.Text = "10"; 
            InitializeComponent();
        }

        private void FiddlerApplication_BeforeRequest(Session oSession)
        {
             if ((!string.IsNullOrEmpty(this.Iptext.Text))&&oSession.m_hostIP.StartsWith(this.Iptext.Text))
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = oSession.url+oSession.m_hostIP;
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
            else
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = oSession.url + oSession.m_hostIP; ;
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
          

        }

 
        private void button1_Click(object sender, EventArgs e)
        {
            this.treeView1.BeginInvoke((ThreadStart)delegate ()
            {
                TreeNode tempNode = new TreeNode();
                tempNode.Text = "开始";
                this.treeView1.Nodes.Add(tempNode);
            });
            if(!Fiddler.FiddlerApplication.IsStarted())
            {
                FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
                Fiddler.FiddlerApplication.Startup(port, true, true);
            }
            else
            {
                this.treeView1.BeginInvoke((ThreadStart)delegate ()
                {
                    TreeNode tempNode = new TreeNode();
                    tempNode.Text = "不能重复开启";
                    this.treeView1.Nodes.Add(tempNode);
                });
            }
 
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.treeView1.BeginInvoke((ThreadStart)delegate ()
            {
                TreeNode tempNode = new TreeNode();
                tempNode.Text = "终止";
                this.treeView1.Nodes.Add(tempNode);
            });
            Fiddler.FiddlerApplication.Shutdown();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
    }

 

 

猜你喜欢

转载自blog.csdn.net/qq_25744257/article/details/84030315