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() 包含你需要的所有请求信息,尽情的挥发吧