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:\t" + 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:\t" + oS.fullUrl);   
  99.                 Console.Title = ("Session list contains: " + oAllSessions.Count.ToString() + " sessions");  
  100.             };  

 

作為學習,注釋的代碼也很重要,大家有必要拿出來運行一下,看看結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM