Use FiddlerCore custom HTTP / HTTPS proxy network

Fiddler is a good use of network requests View and debugging tools, you can write plug-ins to extend its functionality.

Fiddler plug-in development using WPF UI controls as - J. sun cat - blog Park

But in some scenes, you need to customize many network monitoring operations, integrated into their programs. This is where the FiddlerCore.

Fiddler on the use, Fiddler plug-in development, FiddlerCore detailed description, you can see "Fiddler authority Debugging Guide" book, can be found online electronic version.

FiddlerCore installation

The latest version FiddlerCore is the paid version, but nuget.org can download the previous version (Last updated: 2016-01-01), basic function is enough, not directly pull, then you need to configure a local source Nuget and then put the bag Nuget into them.
And csproj project file, add:

    <ItemGroup>
        <PackageReference Include="FiddlerCore" Version="4.6.2.0"/>
    </ItemGroup>

FiddlerCore 官网:FiddlerCore - Fiddler Proxy Engine for .NET and .NET Standard - Telerik
FiddlerCore Nuget: NuGet Gallery | FiddlerCore 4.6.2
FiddlerCore Nuget 包(4.6.2) 下载地址:https://pan.baidu.com/s/1ueTCsJ5Jv7wovCeWVN4gDw

FiddlerCore use

FiddlerCore very simple to use, the main agent is 1 start, 2 listening event, install the certificate 3, 4 close proxy

  • 1 Start Acting
FiddlerApplication.Startup(9898, FiddlerCoreStartupFlags.Default | FiddlerCoreStartupFlags.RegisterAsSystemProxy);

Here FiddlerCoreStartupFlags.RegisterAsSystemProxyis this your program (or 127.0.0.1:9898) registered agent for the system, take all system agents, will through this program.
If not set FiddlerCoreStartupFlags.RegisterAsSystemProxy, were monitoring the program, you need to manually specify the proxy to the 9898 port.

  • 2 monitor events
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.BeforeResponse += FiddlerApplication_BeforeResponse;

private static void FiddlerApplication_BeforeResponse(Session oSession)
{
}

private static void FiddlerApplication_BeforeRequest(Session oSession)
{
}

Through these two events, you can get request FiddlerCore intercepted. Or a recording operation is then performed.

  • 3 Install Certificate

If you need to listen for HTTPS requests, you need to install the certificate, when the certificate is installed, there will be a pop, allowing users to agree.

// 安装证书
public static bool InstallCertificate()
{
    if (!CertMaker.rootCertExists())
    {
        if (!CertMaker.createRootCert())
            return false;

        if (!CertMaker.trustRootCert())
            return false;
    }

    return true;
}
// 卸载证书
public static bool UninstallCertificate()
{
    if (CertMaker.rootCertExists())
    {
        if (!CertMaker.removeFiddlerGeneratedCerts(true))
            return false;
    }
    return true;
}

  • 4 Close agents

This is important, because if the agent does not shut down after the end of the program, you can not normally access. (Because proxy settings, but the agent closed.)

if (FiddlerApplication.IsStarted())
{
    FiddlerApplication.Shutdown();
}

When you actually use these operations, and recommends a layer of abstraction and encapsulation, or too tight business and network monitoring code coupling.

More about the realization of network test and monitoring, you can refer to this project:
RickStrahl / WestWindWebSurge: Quick and the Easy and the URL of the Load Testing for Web Applications ON your Windows

Reference material

Original link: https://www.cnblogs.com/jasongrass/p/12044321.html

Guess you like

Origin www.cnblogs.com/jasongrass/p/12044321.html