Use FiddlerCore to test WebAPI, fiddlercorewebapi

Source: Internet
Author: User

Use FiddlerCore to test WebAPI, fiddlercorewebapi

When debugging Web-related APIs, you often use Fiddler to view related requests and return results. Of course, you can try to modify or repeat your request information. This article describes how to use code to implement the fiddler function.

Fiddler Core API

Fiddler Core provides almost all the functions you can do with fiddler. Search for FiddlerCore on NuGet to download the. Net API of FiddlerCore.

Enable Fiddler Application

Use the following code to enable FiddlerApplication

FiddlerApplication. Startup (9898, FiddlerCoreStartupFlags. Default );

After the agent is executed, fiddler runs an http Proxy Server. You can use FiddlerCoreStartupFlags. RegisterAsSystemProxy to specify the proxy server as a system proxy, so that you can listen to all the http requests on the local machine.

When the program ends, remember to use the following statement to close the proxy.

FiddlerApplication. Shutdown ();

Capture HttpRequest/HttpResponse

After the Fiddler Application is enabled, Fiddler triggers the following two events when capturing Request/Response. You only need to define the event to process the captured Request.

//

// Summary:

// This event fires when a client request is received ed by Fiddler

Public static event SessionStateHandler BeforeRequest;

//

// Summary:

// This event fires when a server response is already ed by Fiddler

Public static event SessionStateHandler BeforeResponse;

Install Certificate

How can I capture https pages? As we all know, https uses communication certificates to encrypt servers and clients to avoid listening during communication. Fiddler uses the man-in-the-middle method to capture the https protocol. The man-in-the-middle means that Fiddler is injected into the middle of the application and server. fiddler plays the client role relative to the server and the server role relative to the client, since fiddler needs to assume the role of the server, it requires a certificate, and your client needs to trust the certificate of Fiddler. Take the website of The Bank of China as an example:

When you do not enable Fiddler to log on to online banking, the certificate information is:

 

After you enable Fiddler to log on to the online banking system, the certificate information is:

Since my machine has trusted the Fiddler certificate, we can find that after the Fiddler is enabled, the communication certificate of the Bank and the Bank has changed to: DO_NOT_TRUST_FiddlerRoot. If you use FiddlerCore, you also need to trust this certificate. The related code is as follows:

public static bool InstallCertificate(){    if (!CertMaker.rootCertExists())    {        if (!CertMaker.createRootCert())            return false;        if (!CertMaker.trustRootCert())            return false;    }    return true;}
Start capture

By using this method, you can test whether the returned results of your API are correct without changing your existing code. The following example uses the FiddlerCoreAPI to test whether the SharePointOnline authentication is successful.

Using Fiddler; using Microsoft. sharePoint. client; 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 cret = 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) {// you can modify the Response information to use the Console at will. writeLine ("BeforeResponse: {0}", oSession. responseCode);} static void FiddlerApplication_BeforeRequest (Session oSession) {// you can modify the Request information to use the Console at will. writeLine ("BeforeRequest: {0}, {1}", oSession. fullUrl, oSession. responseCode );}}}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.