【转】使用FiddlerCore来测试WebAPI
大家在調試Web相關的API時,經常會用Fiddler來查看相關的請求,以及返回結果。當然你也可以嘗試修改或者重復發送你的請求信息。本文主要介紹如何使用代碼來實現fiddler的功能。
Fiddler Core API
Fiddler Core幾乎實現了你能用fiddler做的所有功能。直接在NuGet上搜索FiddlerCore即可下載FiddlerCore的.Net API。
開啟Fiddler Application
使用下面的代碼來開啟FiddlerApplication
FiddlerApplication.Startup(9898,?FiddlerCoreStartupFlags.Default);
執行后,fiddler會運行一個http代理服務器,你可以使用FiddlerCoreStartupFlags.RegisterAsSystemProxy 來把這個代理服務器指定為系統代理,這樣就可以監聽到本機所有的http請求。
當程序結束的時候,記得使用下面的語句來關閉代理。
FiddlerApplication.Shutdown();
捕獲HttpRequest/HttpResponse
開啟了Fiddler Application之后,Fiddler在捕獲Request/Response的時候會觸發下面這兩個事件,你只需要定義事件來實現如何處理捕獲到的請求即可。
//
// Summary:
// This event fires when a client request is received by Fiddler
public?static?event?SessionStateHandler?BeforeRequest;
//
// Summary:
// This event fires when a server response is received by Fiddler
public?static?event?SessionStateHandler?BeforeResponse;
安裝證書
那么如何捕獲https協議的頁面呢?眾所周知,https通過通信證書來實現了服務器端和客戶端的加密,避免通信過程被監聽。Fiddler通過中間人的方式來實現https協議的捕獲,所謂中間人就是Fiddler注入到應用程序和服務器的中間,fiddler相對于服務器扮演客戶端的角色,相對于客戶端扮演服務器的角色,既然fiddler需要扮演服務器的角色,就需要一個證書,并且你的客戶端需要信任Fiddler的證書。我們以中國銀行的網站為例:
不開啟Fiddler登陸網銀時,證書信息為:
?
開啟Fiddler登陸網銀后證書信息為:
由于我的機器已經信任過Fiddler的證書,我們可以發現,在開啟了Fiddler后,和中行網銀的通信證書變為了:DO_NOT_TRUST_FiddlerRoot。如果使用FiddlerCore,我們同樣需要信任這個證書,相關的代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | public?static?bool?InstallCertificate() { ????if?(!CertMaker.rootCertExists()) ????{ ????????if?(!CertMaker.createRootCert()) ????????????return?false; ? ????????if?(!CertMaker.trustRootCert()) ????????????return?false; ????} ? ????return?true; } |
開始捕獲
使用這種方式,可以在不改變你現有代碼的情況下,測試你的API返回結果是否正確。下面的例子是一個用FiddlerCoreAPI來測試SharePointOnline認證是否通過的例子。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | using?Fiddler; using?Microsoft.SharePoint.Client; using?System; using?System.Collections.Generic; using?System.IO; using?System.Linq; using?System.Net; using?System.Security; using?System.Text; using?System.Threading.Tasks; ? namespace?FiddlerCoreTest { ????class?Program ????{ ????????static?void?Main(string[] args) ????????{ ????????????ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) =>?true; ? ????????????FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest; ????????????FiddlerApplication.BeforeResponse += FiddlerApplication_BeforeResponse; ????????????FiddlerApplication.Startup(9898, FiddlerCoreStartupFlags.Default | FiddlerCoreStartupFlags.RegisterAsSystemProxy); ????????????try ????????????{ ????????????????ClientContext context =?new?ClientContext("https://domain.sharepoint.com"); ? ????????????????SecureString se =?new?SecureString(); ????????????????foreach?(var?cc?in?"password") ????????????????{ ????????????????????se.AppendChar(cc); ????????????????} ? ????????????????var?cre =?new?SharePointOnlineCredentials("user@domain.onmicrosoft.com", se); ????????????????var?cookie = cre.GetAuthenticationCookie(new?Uri("https://domain.sharepoint.com")); ????????????} ????????????catch?(Exception e) ????????????{ ? ????????????} ? ????????????FiddlerApplication.Shutdown(); ????????????Console.ReadLine(); ????????} ? ????????static?void?FiddlerApplication_BeforeResponse(Session oSession) ????????{ ????????????//想如何改寫Response信息在這里隨意發揮了 ????????????Console.WriteLine("BeforeResponse: {0}", oSession.responseCode); ????????} ? ????????static?void?FiddlerApplication_BeforeRequest(Session oSession) ????????{ ????????????//想如何改寫Request信息在這里隨意發揮了 ????????????Console.WriteLine("BeforeRequest: {0}, {1}", oSession.fullUrl, oSession.responseCode); ????????} ????} } |
?
作者:獨上高樓
出處:http://www.cnblogs.com/myprogram/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
總結
以上是生活随笔為你收集整理的【转】使用FiddlerCore来测试WebAPI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】ABP源码分析三十七:ABP.We
- 下一篇: 【转】ABP源码分析二十七:ABP.En