zoukankan      html  css  js  c++  java
  • FiddlerCoreAPI 使用简介

    原文:https://blog.csdn.net/zhang116868/article/details/49406599

    大名鼎鼎的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实例来进行讲解。

    启动方法:

    [csharp] view plain copy
     
    1. //设置别名  
    2.  Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreDemoApp");  
    3.   
    4. //启动方式  
    5. FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;  
    6.   
    7. //定义http代理端口  
    8. int iPort = 8877;  
    9. //启动代理程序,开始监听http请求  
    10. //端口,是否使用windows系统代理(如果为true,系统所有的http访问都会使用该代理)  
    11. Fiddler.FiddlerApplication.Startup(iPort, true, false, true);  
    12.   
    13. // 我们还将创建一个HTTPS监听器,当FiddlerCore被伪装成HTTPS服务器有用  
    14. // 而不是作为一个正常的CERN样式代理服务器。  
    15. oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iSecureEndpointPort, true, sSecureEndpointHostname);  


    停止方法:

    [csharp] view plain copy
     
    1. if (null != oSecureEndpoint) oSecureEndpoint.Dispose();  
    2. Fiddler.FiddlerApplication.Shutdown();  
    3. Thread.Sleep(500);  


    如果占用了系统代理,那么一定要记得如此退出,不然系统代理不会被释放,你将不能再打开网页。


    接下来介绍拦截http请求的处理:

    [csharp] view plain copy
     
    1. //定义会话,每一个请求都将封装成一个会话  
    2. List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();  
    3.   
    4.             //请求出错时处理  
    5.             Fiddler.FiddlerApplication.BeforeReturningError += FiddlerApplication_BeforeReturningError;  
    6.   
    7.             //在发送请求之前执行的操作  
    8.             Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)  
    9.             {  
    10.                 //请求的全路径  
    11.                 Console.WriteLine("Before request for: " + oS.fullUrl);  
    12.                 // 为了使反应篡改,必须使用缓冲模式  
    13.                 // 被启用。这允许FiddlerCore以允许修改  
    14.                 // 在BeforeResponse处理程序中的反应,而不是流  
    15.                 // 响应给客户机作为响应进来。  
    16.   
    17.                 Debug.WriteLine(oS.RequestBody);  
    18.   
    19.                 oS.bBufferResponse = true;  
    20.                 Monitor.Enter(oAllSessions);  
    21.                 oAllSessions.Add(oS);  
    22.                 Monitor.Exit(oAllSessions);  
    23.                 oS["X-AutoAuth"] = "(default)";  
    24.   
    25.                 /* 如果请求是要我们的安全的端点,我们将回显应答。 
    26.                  
    27.                 注:此BeforeRequest是越来越要求我们两个主隧道代理和我们的安全的端点, 
    28.                 让我们来看看它的Fiddler端口连接到(pipeClient.LocalPort)客户端,以确定是否该请求 
    29.                 被发送到安全端点,或为了达到**安全端点被仅仅发送到主代理隧道(例如,一个CONNECT)。 
    30.  
    31.                 因此,如果你运行演示和参观的https://本地主机:7777在浏览器中,你会看到 
    32.  
    33.                 Session list contains... 
    34.                   
    35.                     1 CONNECT http://localhost:7777 
    36.                     200                                         <-- CONNECT tunnel sent to the main proxy tunnel, port 8877 
    37.  
    38.                     2 GET https://localhost:7777/ 
    39.                     200 text/html                               <-- GET request decrypted on the main proxy tunnel, port 8877 
    40.  
    41.                     3 GET https://localhost:7777/                
    42.                     200 text/html                               <-- GET request received by the secure endpoint, port 7777 
    43.                 */  
    44.   
    45.                 //oS.utilCreateResponseAndBypassServer();  
    46.                 //oS.oResponse.headers.SetStatus(200, "Ok");  
    47.                 //string str = oS.GetResponseBodyAsString();  
    48.                 //oS.utilSetResponseBody(str + "aaaaaaaaaaaaaaaaaaaaa");  
    49.   
    50.                 if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (oS.hostname == sSecureEndpointHostname))  
    51.                 {  
    52.                     oS.utilCreateResponseAndBypassServer();  
    53.                     oS.oResponse.headers.SetStatus(200, "Ok");  
    54.                     oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";  
    55.                     oS.oResponse["Cache-Control"] = "private, max-age=0";  
    56.                     oS.utilSetResponseBody("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());  
    57.                 }  
    58.                 //if ((oS.oRequest.pipeClient.LocalPort == 8877) && (oS.hostname == "www.baidu.com"))  
    59.                 //{  
    60.                 //    string url = oS.fullUrl;  
    61.                 //    oS.utilCreateResponseAndBypassServer();  
    62.                 //    oS.oResponse.headers.SetStatus(200, "Ok");  
    63.                 //    oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";  
    64.                 //    oS.oResponse["Cache-Control"] = "private, max-age=0";  
    65.                 //    oS.utilSetResponseBody("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());  
    66.                 //}  
    67.             };  
    68.   
    69.             /* 
    70.                 // 下面的事件,您可以检查由Fiddler阅读每一响应缓冲区。   
    71.              *     请注意,这不是为绝大多数应用非常有用,因为原始缓冲区几乎是无用的;它没有解压,它包括标题和正文字节数等。 
    72.                 // 
    73.                 // 本次仅适用于极少数的应用程序这就需要一个原始的,未经处理的字节流获取有用 
    74.                 Fiddler.FiddlerApplication.OnReadResponseBuffer += new EventHandler<RawReadEventArgs>(FiddlerApplication_OnReadResponseBuffer); 
    75.             */  
    76.   
    77.   
    78.             Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS) {  
    79.                   
    80.                   
    81.                 // 取消以下两条语句解压缩/ unchunk的  
    82.                 //HTTP响应,并随后修改任何HTTP响应,以取代  
    83.                 //单词“微软”和“Bayden”的实例。 你必须如此设置:  
    84.                 // set bBufferResponse = true inside the beforeREQUEST method above.  
    85.                 //  
    86.                 oS.utilDecodeResponse();  
    87.                 // 内容:{3} , oS.GetResponseBodyEncoding().GetString(oS.responseBodyBytes)  
    88.                 Console.WriteLine("{0}:HTTP {1} for {2}", oS.id, oS.responseCode, oS.fullUrl);  
    89.                 Console.WriteLine(oS.GetResponseBodyAsString());  
    90.                 Debug.WriteLine(oS.GetResponseBodyAsString());  
    91.                 //bool r = oS.utilReplaceInResponse("1.欢迎使用!", "aaaaaaaaaaaaaaaaaaaaaa");  
    92.                 //Console.WriteLine(r);  
    93.   
    94.             };  
    95.   
    96.             Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)  
    97.             {  
    98.                 //Console.WriteLine("Finished session: " + oS.fullUrl);   
    99.                 Console.Title = ("Session list contains: " + oAllSessions.Count.ToString() + " sessions");  
    100.             };  

    作为学习,注释的代码也很重要,大家有必要拿出来运行一下,看看结果。

  • 相关阅读:
    自动化设计自动化测试介绍
    自动化设计框架介绍 TestReport
    自动化设计自动化测试环境搭建<二>
    自动化设计自动化测试环境搭建<三>
    浅谈敏捷模型
    自动化设计框架介绍 TestLog
    自动化设计框架介绍 TestScript
    自动化设计框架介绍
    LoadRunner脚本录制常见问题整理<转>
    自动化设计框架介绍 TestSnap
  • 原文地址:https://www.cnblogs.com/itfat/p/9103520.html
Copyright © 2011-2022 走看看