首先从官网下载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)
{
}
}
2 获取拦截请求的域名和ip
private void FiddlerApplication_BeforeRequest(Session oS)
{
if ((!string.IsNullOrEmpty(this.txt_destinationHost.Text)) && oSession.m_hostIP.StartsWith(this.txt_destinationHost.Text))
{
}
}
3 修改响应内容,修改返回请求头,状态,内容
private void FiddlerApplication_BeforeRequest(Session oS)
{
if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (oS.hostname == sSecureEndpointHostname))
{
oS.utilCreateResponseAndBypassServer();
oS.oResponse.headers.SetStatus(200, "Ok");
oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
oS.oResponse["Cache-Control"] = "private, max-age=0";
var Restr="返回内容";
oS.utilDecodeResponse();
oS.utilSetResponseBody(Restr);
}
}
4 开启FiddlerCore在指定端口的监听。
Fiddler.FiddlerApplication.Startup(iPort, oFCSF);
5 建立在指定端口的HTTPS的监听
oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);
6关闭应用
Fiddler.FiddlerApplication.Shutdown();
7获取请求头信息和请求内容
oS.RequestHeaders.ToByteArray(true, true, !oS.isHTTPS)
oS.RequestBody
8 设置代理网关 提供主机:网关的端口组合,该网关应该用于代理此请求,或直接将请求发送到原始服务器。
x-overrideGateway
oSession["X-OverrideGateway"] ="socks=127:80"