FiddlerCoreAPI 使用简介
2015-10-25 20:56
471 查看
大名鼎鼎的Fiddler大家都知道,或者用过,Fiddler 开放了他的FiddlerCoreAPI 提供给开发者调用,来处理所有的http请求,功能就如Fiddler一样强大,下面我们来简单介绍一下。
程序类库官网下载地址:http://fiddler.wikidot.com/fiddlercore-api
官网上下的是exe安装文件,比较麻烦,因此笔者上传了一个安装后提取出来的库,包括.net 2.0、4.0两个运行环境版本和一个简单的实例。
下载地址:http://download.csdn.net/detail/zhang116868/9211923
接下来我们根据官方提供的SampleApp实例来进行讲解。
启动方法:
停止方法:
如果占用了系统代理,那么一定要记得如此退出,不然系统代理不会被释放,你将不能再打开网页。
接下来介绍拦截http请求的处理:
作为学习,注释的代码也很重要,大家有必要拿出来运行一下,看看结果。
转自:FiddlerCoreAPI 使用简介
程序类库官网下载地址:http://fiddler.wikidot.com/fiddlercore-api
官网上下的是exe安装文件,比较麻烦,因此笔者上传了一个安装后提取出来的库,包括.net 2.0、4.0两个运行环境版本和一个简单的实例。
下载地址:http://download.csdn.net/detail/zhang116868/9211923
接下来我们根据官方提供的SampleApp实例来进行讲解。
启动方法:
//设置别名 Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreDemoApp"); //启动方式 FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default; //定义http代理端口 int iPort = 8877; //启动代理程序,开始监听http请求 //端口,是否使用windows系统代理(如果为true,系统所有的http访问都会使用该代理) Fiddler.FiddlerApplication.Startup(iPort, true, false, true); // 我们还将创建一个HTTPS监听器,当FiddlerCore被伪装成HTTPS服务器有用 // 而不是作为一个正常的CERN样式代理服务器。 oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);
停止方法:
if (null != oSecureEndpoint) oSecureEndpoint.Dispose(); Fiddler.FiddlerApplication.Shutdown(); Thread.Sleep(500);
如果占用了系统代理,那么一定要记得如此退出,不然系统代理不会被释放,你将不能再打开网页。
接下来介绍拦截http请求的处理:
//定义会话,每一个请求都将封装成一个会话 List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>(); //请求出错时处理 Fiddler.FiddlerApplication.BeforeReturningError += FiddlerApplication_BeforeReturningError; //在发送请求之前执行的操作 Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS) { //请求的全路径 Console.WriteLine("Before request for:\t" + oS.fullUrl); // 为了使反应篡改,必须使用缓冲模式 // 被启用。这允许FiddlerCore以允许修改 // 在BeforeResponse处理程序中的反应,而不是流 // 响应给客户机作为响应进来。 Debug.WriteLine(oS.RequestBody); oS.bBufferResponse = true; Monitor.Enter(oAllSessions); oAllSessions.Add(oS); Monitor.Exit(oAllSessions); oS["X-AutoAuth"] = "(default)"; /* 如果请求是要我们的安全的端点,我们将回显应答。 注:此BeforeRequest是越来越要求我们两个主隧道代理和我们的安全的端点, 让我们来看看它的Fiddler端口连接到(pipeClient.LocalPort)客户端,以确定是否该请求 被发送到安全端点,或为了达到**安全端点被仅仅发送到主代理隧道(例如,一个CONNECT)。 因此,如果你运行演示和参观的https://本地主机:7777在浏览器中,你会看到 Session list contains... 1 CONNECT http://localhost:7777 200 <-- CONNECT tunnel sent to the main proxy tunnel, port 8877 2 GET https://localhost:7777/ 200 text/html <-- GET request decrypted on the main proxy tunnel, port 8877 3 GET https://localhost:7777/ 200 text/html <-- GET request received by the secure endpoint, port 7777 */ //oS.utilCreateResponseAndBypassServer(); //oS.oResponse.headers.SetStatus(200, "Ok"); //string str = oS.GetResponseBodyAsString(); //oS.utilSetResponseBody(str + "aaaaaaaaaaaaaaaaaaaaa"); 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("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString()); } //if ((oS.oRequest.pipeClient.LocalPort == 8877) && (oS.hostname == "www.baidu.com")) //{ // string url = oS.fullUrl; // 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("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString()); //} }; /* // 下面的事件,您可以检查由Fiddler阅读每一响应缓冲区。 * 请注意,这不是为绝大多数应用非常有用,因为原始缓冲区几乎是无用的;它没有解压,它包括标题和正文字节数等。 // // 本次仅适用于极少数的应用程序这就需要一个原始的,未经处理的字节流获取有用 Fiddler.FiddlerApplication.OnReadResponseBuffer += new EventHandler<RawReadEventArgs>(FiddlerApplication_OnReadResponseBuffer); */ Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS) { // 取消以下两条语句解压缩/ unchunk的 //HTTP响应,并随后修改任何HTTP响应,以取代 //单词“微软”和“Bayden”的实例。 你必须如此设置: // set bBufferResponse = true inside the beforeREQUEST method above. // oS.utilDecodeResponse(); // 内容:{3} , oS.GetResponseBodyEncoding().GetString(oS.responseBodyBytes) Console.WriteLine("{0}:HTTP {1} for {2}", oS.id, oS.responseCode, oS.fullUrl); Console.WriteLine(oS.GetResponseBodyAsString()); Debug.WriteLine(oS.GetResponseBodyAsString()); //bool r = oS.utilReplaceInResponse("1.欢迎使用!", "aaaaaaaaaaaaaaaaaaaaaa"); //Console.WriteLine(r); }; Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS) { //Console.WriteLine("Finished session:\t" + oS.fullUrl); Console.Title = ("Session list contains: " + oAllSessions.Count.ToString() + " sessions"); };
作为学习,注释的代码也很重要,大家有必要拿出来运行一下,看看结果。
转自:FiddlerCoreAPI 使用简介
相关文章推荐
- c++和java的区别
- OOD沉思录 --- 继承
- 网页复制回车 替换为word的回车
- 解决/阻止 div层内的div层点击事件冒泡触发的小方法
- LEETCODE-Count and Say
- DOM常用对象
- 关于makefile的=: =? =+区别
- 【机器学习算法】之决策树
- 公钥和私钥
- 工厂模式
- OOD沉思录 --- 导引
- TypeScript学习笔记(四):函数
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-18-Noise Layers
- 数据库范式
- 拓扑排序
- 嵌入式Linux根文件系统构建
- 《编程之法》练习题-1.1字符串的旋转
- mysql 性能优化方案
- ESP8266上的FreeRTOS初体验
- PyQt4 ShowHMDB show sqlite3 with QTableWidget summary