FiddlerCore-C#爬虫

Fiddler为FiddlerCore的UI版本,Fiddler4之后的版本是付费的,仅仅为了扒资源又去下载个付费版太不值了,FiddlerCore4.0这个版本的坑还是挺多的,本人也是从踩坑又爬坑又踩坑......

废话不多说,上代码

using System;
using Fiddler;
using BCCertMaker;
using System.Windows.Forms;
using System.Collections.Generic;

namespace SGM_GATEWAY
{
    class FiddlerUtil
    {
        //代理端口
        private static int proxyPort = 9937;
        //启动端口
        private static int startPort = 9936;
        private static bool isIgnoreCertError = false;
        // 代理
        private static Proxy proxy;
        private static string proxyHost = "localhost";
        private static bool isStart = false;
        public static Login login;

        public static bool start(int type,FormMain formMain)
        {
            TYPE = type;
            try
            {
                if (FiddlerApplication.IsStarted())
                {
                    close(formMain,false);
                }
                // 设置证书
                if (!Fiddler.CertMaker.rootCertIsMachineTrusted()) {
                    BCCertMaker.BCCertMaker a = new BCCertMaker.BCCertMaker();
                    a.CreateRootCertificate();
                    a.TrustRootCertificate();
                    isFirstCreateCert = false;
                }
                // 忽略证书错误
                CONFIG.IgnoreServerCertErrors = true;
                CONFIG.bCaptureCONNECT = true;

                Fiddler.CONFIG.IgnoreServerCertErrors = isIgnoreCertError;
                //创建一个https侦听器,用于伪装成https服务器
                FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);
                //启动侦听
                FiddlerApplication.Startup(startPort, true,true);
                FormUtil.appendConsole(formMain, "正在启动代理...");
                //注册事件,用于捕获网络流量
                    FiddlerApplication.BeforeResponse += new SessionStateHandler(delegate (Session session) {
                        loginRes(session, formMain);
                    });
                // 代理
                proxy = FiddlerApplication.CreateProxyEndpoint(proxyPort, true, proxyHost);
                FormUtil.appendConsole(formMain, "代理启动完成...");
                FormUtil.appendConsole(formMain, isStart?"重启成功!": ("Fiddler启动成功,端口:" + startPort));
                if (null != formMain)
                {
                    formMain.fiddlerStart.Text = "重启";
                }
                isStart = true;
                return true;
            }
            catch (Exception e) {
                e.GetType();
                close(formMain,false);
                MsgBox.Show("Fiddler启动异常,请重启计算机!", "提示",MsgButton.OK);
                return false;
            }
        }

        public static void close(FormMain formMain,bool isClick)
        {
            FiddlerUtil.TYPE = 0;
            if (Fiddler.FiddlerApplication.IsStarted())
            {
                //停止Fiddler
                Fiddler.FiddlerApplication.Shutdown();
                if (isClick) {
                    FormUtil.appendConsole(formMain, "Fiddler关闭成功,端口:" + startPort);
                    formMain.fiddlerStart.Text = "启动";
                    isStart = false;
                }
            }
            else
            {
                FormUtil.appendConsole(formMain, "Fiddler未启动");
            }
            if (null != proxy) {
                proxy.Dispose();
            }
        }

        private static string pageSessionKey = "x-pagesession-code:";
        private static string cookieKey = "Cookie:";

        
        // 登录监听
        private static void loginRes(Session session, FormMain formMain) {
            
        }
    }
}

session.ToString() 包含你需要的所有请求信息,尽情的挥发吧

Fiddler Core套装ddl(包含证书类ddl,点此下载)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值