FiddlerCoreAPI开发(一)源码分析
1.前言
前一段时间想利用fiddlercore截取本地HTTPS的流量做一些分析,按照样例代码的注释学习了一下,没搞清楚怎么实现,后来又在网上查了些资料,对HTTPS的处理提及很少,都没有解决我的问题,主要是HTTPS证书的问题,索性自己研究了一下,终于解决了问题。我会在下篇文章中分享下我的思路,本篇文章先简单分析下fiddlercore自带样例的代码,帮助刚接触fiddlercore的人快速入门,如果有说的不对的地方,欢迎批评指正。
2.源码分析
首先从官网下载FiddlerCoreAPI
https://www.telerik.com/purchase/fiddlercore
下载下来是一个安装文件,解压后有demo和FiddlerCoreAPI库,打开样例代码工程,开始分析。
从主函数开始。
List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();
定义了一个fiddler的Session类的List,里面存放的是客户端和服务端的消息。
Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreDemoApp");
命名自己的应用程序。
Fiddler.FiddlerApplication.OnNotification += delegate(object sender, NotificationEventArgs oNEA) { Console.WriteLine("** NotifyUser: " + oNEA.NotifyString); };
Fiddler.FiddlerApplication.Log.OnLogString += delegate(object sender, LogEventArgs oLEA) { Console.WriteLine("** LogString: " + oLEA.LogString); };
这两句,第一句绑定了用户通知事件的函数,具体什么时候触发没仔细研究,第二句绑定了FiddlerApplication.Log触发的事件(FiddlerCore自己的日志系统),后面打印内容都会用到,总之这两句就是把内容打印在控制台上。
Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
{
// Console.WriteLine("Before request for:\t" + oS.fullUrl);
oS.bBufferResponse = false;
Monitor.Enter(oAllSessions);
oAllSessions.Add(oS);
Monitor.Exit(oAllSessions);
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";
oS.utilSetResponseBody("Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:

浙公网安备 33010602011771号