When you debug Web-related APIs, you often use Fiddler to view related requests and return results. Of course you can also try to modify or repeat your request information. This article mainly describes how to use code to implement fiddler functionality.
Fiddler Core API
Fiddler Core almost implements all the features you can do with Fiddler. Search Fiddlercore directly on NuGet to download the Fiddlercore. Net API.
Open Fiddler Application
Use the following code to turn on Fiddlerapplication
fiddlerapplication. Startup (9898, fiddlercorestartupflags. Default);
After execution, fiddler will run an HTTP proxy server, you can use Fiddlercorestartupflags.registerassystemproxy to designate this proxy server as a System agent. This allows you to listen to all of the HTTP requests on this machine.
When the program finishes, remember to use the following statement to close the agent.
fiddlerapplication. Shutdown ();
Capture Httprequest/httpresponse
After fiddler application is turned on, fiddler will trigger the following two events when capturing Request/response, and you only need to define the event to implement how the captured request is handled.
//
//Summary:
//This event fires when a client request was received by Fiddler
public static event sessionstatehandler beforerequest;
//
//Summary:
//This event fires when a server response was received by Fiddler
Public static event sessionstatehandler beforeresponse;
Install the certificate
So how do you capture the HTTPS protocol page? As we all know, HTTPS through the communication certificate to achieve the server side and client encryption, to avoid the communication process is monitored. Fiddler the use of intermediaries to achieve the capture of the HTTPS protocol, the so-called middleman is fiddler injected into the middle of the application and server, Fiddler relative to the server play the role of the client, relative to the client play the role of the server, Now that Fiddler needs to play the role of a server, you need a certificate, and your client needs to trust Fiddler's certificate. We take the Bank of China website as an example:
When you do not turn on fiddler, the Certificate information is:
Open Fiddler Login Net Silver certificate information is:
Since my machine has trusted Fiddler's certificate, we can find that after opening the fiddler, and the Bank of BOC's communication certificate changed to: Do_not_trust_fiddlerroot. If we use Fiddlercore, we also need to trust this certificate, the relevant code is as follows:
public static bool Installcertificate () { if (! Certmaker.rootcertexists ()) { if (! Certmaker.createrootcert ()) return false; if (! Certmaker.trustrootcert ()) return false; } return true;}
Start capturing
In this way, you can test whether your API returns the correct results without changing your existing code. The following example is an example of using FIDDLERCOREAPI to test whether a sharepointonline certification is passed.
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.serv Ercertificatevalidationcallback = (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 ("[email protected]", SE); var cookie = cre. Getauthenticationcookie (New Uri ("https://domain.sharepoint.com")); } catch (Exception e) {} fiddlerapplication.shutdown (); Console.ReadLine (); } static void Fiddlerapplication_beforeresponse (Session osession) {//How to rewrite response information here at random Console.WriteLine ("Beforeresponse: {0}", Osession.responsecode); } static void Fiddlerapplication_beforerequest (Session osession) {//How to rewrite the request information here at random Console.WriteLine ("Beforerequest: {0}, {1}", Osession.fullurl, Osession.responsecode); } }}
Use Fiddlercore to test Webapi