FiddlerCore是收费软件,提供了Fiddler的抓包API,能抓取HTTP和HTTPS的网络请求。
官网:https://www.telerik.com/fiddlercore
安装FiddlerCore依赖引用有两种方式:
1、通过Telerik NuGet Server下载。这种方式需要注册账号,一般也会下载失败。
2、通过VS引用FiddlerCore.dll。
实现界面:
实现代码:
using Fiddler;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string tmp = "-------------------------------------";
private void btn_start_Click(object sender, EventArgs e)
{
startFiddler();
}
// 启动捕获
public void startFiddler()
{
if (!FiddlerApplication.IsStarted())
{
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
}
else
{
appendtext("fiddler is running...");
}
}
// 请求前回调
private void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
string url = oSession.fullUrl;
string body = oSession.GetRequestBodyAsString();
string header = oSession.RequestHeaders.ToString();
appendtext(url);
appendtext(header);
appendtext(body);
}
}
// 会话结束后回调
private void FiddlerApplication_AfterSessionComplete(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
string body = oSession.GetResponseBodyAsString();
appendtext(body);
appendtext(tmp);
}
}
// 停止捕获
public void stopFiddler()
{
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
// 安装证书
public void install()
{
if (!CertMaker.rootCertExists())
{
CertMaker.createRootCert();
CertMaker.trustRootCert();
}
}
// 证书移除
public void remove()
{
if (CertMaker.rootCertExists())
{
CertMaker.removeFiddlerGeneratedCerts();
}
}
public void appendtext(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action(() =>
{
richTextBox1.AppendText(value + "\r\n");
}));
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
stopFiddler();
}
private void btn_install_Click(object sender, EventArgs e)
{
install();
}
private void btn_remove_Click(object sender, EventArgs e)
{
remove();
}
// 关闭窗口
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
stopFiddler();
if (FiddlerApplication.oProxy != null)
{
if (FiddlerApplication.oProxy.IsAttached)
FiddlerApplication.oProxy.Detach();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.splitContainer1.SplitterDistance = this.Height / 7;
}
}
}
参考: https://docs.telerik.com/fiddlercore/getting-started/telerik-nuget-server