相关文章推荐
深沉的台灯  ·  java开发_UUID(Universall ...·  7 月前    · 
任性的鸡蛋  ·  js匿名函数传参_java ...·  1 年前    · 
爱旅游的蛋挞  ·  VB.NET Form Controls ...·  1 年前    · 

Fiddler自定义脚本可以实现很强大的内容替换,包括很有意义的——修改返回内容。

具体的方法可以参考官网:http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse

而这里想说的是,官网的说明并不准确,可能旧版本Fiddler是没问题的,但在4.X,我发现只修改 OnBeforeResponse 的脚本是无法实现效果的,虽然Fiddler的抓包看起来是成功修改了返回内容,但实际上,浏览器得到的数据还是跟服务器原来返回的一样。

说这么多,遇到问题的同学自然懂,如果没遇到问题的,就直接当我说废话好了。

这里纯属分享,希望帮助到同样遇到困难的人。翻了一圈google都没发现类似的问题,最后遇到这个:http://www.telerik.com/forums/code-working-in-fiddler-but-not-working-in-fiddlercore,也是一个国人找到Fiddler作者的提问。

问题关键点是: 必须在 OnBeforeResponse前, 设置oSession.bBufferResponse = true;

顾名思义,开启了缓存模式来处理返回内容,才能最终反馈到浏览器上,否则,保持原有的流式模式的话,就会出现修改和返回同时进行,浏览器得到的还是原版的数据。

建议在 OnPeekAtResponseHeaders中根据需要,设置 bBufferResponse ,例如我的代码:

    static function OnPeekAtResponseHeaders(oSession: Session) {
        //FiddlerApplication.Log.LogFormat("Session {0}: Response header peek shows status is {1}", oSession.id, oSession.responseCode);
        if (m_DisableCaching) {
            oSession.oResponse.headers.Remove("Expires");
            oSession.oResponse["Cache-Control"] = "no-cache";
        if ((bpStatus>0) && (oSession.responseCode == bpStatus)) {
            oSession["x-breakresponse"]="status";
            oSession.bBufferResponse = true;
        if ((null!=bpResponseURI) && oSession.uriContains(bpResponseURI)) {
            oSession["x-breakresponse"]="uri";
            oSession.bBufferResponse = true;
        if (oSession.HostnameIs("cmshow.qq.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
            oSession.bBufferResponse = true;    //需要在返回头这里就设置buffer处理,否则,后续无法在onBeforeResponse中修改body(修改的动作不会阻塞原来的返回)
    static function OnBeforeResponse(oSession: Session) {
        if (m_Hide304s && oSession.responseCode == 304) {
            oSession["ui-hide"] = "true";
        }if (oSession.HostnameIs("cmshow.qq.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
            oSession.utilDecodeResponse();
            if(NO_SSO){
                oSession.utilReplaceInResponse('</head>','<script>window.nosso = true;</script></head>');
            if(enable_LOG){
                oSession.utilReplaceInResponse('</head>','<script>window.debug = true;</script></head>');
            oSession.utilReplaceInResponse('Content-Security-Policy','');