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





