使用FiddlerCore修改响应

本文介绍了如何在Fiddler中通过设置`bBufferResponse`为true,配合`ResponseHeadersAvailable`和`BeforeResponse`事件,对example.com上的GET请求进行响应体的修改,以实现对原始响应的缓冲和定制。

要修改HTTP响应,请确保会话设置为缓冲模式。

使用响应的一些早期事件,例如,ResponseHeadersAvailable访问会话对象并将bBufferResponse属性设置为true。如果属性没有设置为true,在BeforeResponse事件中,您将收到响应的副本,同时它也将流传输到客户端。因此,您的修改将不会应用于客户端将收到的响应。

private static void FiddlerApplication_ResponseHeadersAvailable(Session oSession)
{

    if (oSession.fullUrl.Contains("example.com"))
    {
        /*
        Set this to true, so in BeforeResponse, you'll be able to modify the body.
        If the value is false (default one), the response you'll work with within the BeforeResponse handler will be just a copy. 
        The original one will already be streamed to the client, and all of your modifications will not be visible there.
        */
        oSession.bBufferResponse = true;
    }
}

然后,您可以处理BeforeResponse事件并根据您的需求修改会话的响应。

private static void FiddlerApplication_BeforeResponse(Session oSession)
{
    if (oSession.fullUrl.Contains("example.com") && oSession.HTTPMethodIs("GET"))
    {
        oSession.bBufferResponse = true;
        oSession.utilDecodeResponse();

        // Remove any compression or chunking
        oSession.utilDecodeResponse();
        var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

        // Modify the body as you want
        oBody = "Replaced body";

        // Set the response body to the div-less string
        oSession.utilSetResponseBody(oBody);
    }
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abbcccddddee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值