BeforeResponse no work in my code.

46 views
Skip to first unread message

jhon1...@hotmail.com

unread,
Dec 19, 2016, 11:45:31 AM12/19/16
to Fiddler
Hi guy , I'm new here.
I want will edit the content response but not does work


using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using Fiddler;

namespace Demo
{
    class Program
    {
        static Proxy oSecureEndpoint;
        static string sSecureEndpointHostname = "localhost";
        static int iPort = 8877;

        private static string Ellipsize(string s, int iLen)
        {
            if (s.Length <= iLen) return s;
            return s.Substring(0, iLen - 3) + "...";
        }

        private static void WriteSessionList(List<Fiddler.Session> oAllSessions)
        {
            try
            {
                Monitor.Enter(oAllSessions);
                foreach (Session oS in oAllSessions)
                {
                    string bodyString =  oS.GetRequestBodyAsString();
                    Console.Write(String.Format("{0} {1} {2}\n{3} {4} {5}\n\n", oS.id, oS.oRequest.headers.HTTPMethod, Ellipsize(oS.fullUrl, 60), oS.responseCode, oS.oResponse.MIMEType, bodyString));
                }
            }
            finally
            {
                Monitor.Exit(oAllSessions);
            }
        }
       
        static void Main(string[] args)
        {

          

          

            #region AttachEventListeners

            Fiddler.FiddlerApplication.BeforeResponse +=  delegate (Fiddler.Session oS)
            {
                if (oS.RequestHeaders.HTTPMethod.ToString() == "GET" || oS.RequestHeaders.HTTPMethod.ToString() == "POST")
                {
                    if (oS.responseCode.ToString() == "200")
                    {
                        if (oS.oResponse.headers.ExistsAndContains("Content-Type", "text/html"))
                        {

                            oS.utilDecodeResponse();
                            var oBody = System.Text.Encoding.UTF8.GetString(oS.responseBodyBytes);
                            oBody += "<h1>My PROXY</h1>";
                            oS.utilSetResponseBody(oBody + oS.oRequest.headers.ToString());


                        }
                    }
                }
            };
            #endregion AttachEventListeners
            #region AttachEventListeners
            Fiddler.FiddlerApplication.BeforeRequest += delegate (Fiddler.Session oS)
            {

                oS.bBufferResponse = false;
                Monitor.Enter(oAllSessions);
                if(oS.oRequest.headers.HTTPMethod.ToString() == "POST")
                {
                    Console.WriteLine();
                    tw.WriteLine(oS);
                }
                
                Monitor.Exit(oAllSessions);
                if ((oS.oRequest.pipeClient.LocalPort == iPort) && (oS.hostname == sSecureEndpointHostname))
                {
                    oS.utilCreateResponseAndBypassServer();
                    oS.oResponse.headers.SetStatus(200, "Ok");
                    oS.oResponse["Content-Type"] = "text/html; charset=UTF-8";
                    oS.oResponse["Cache-Control"] = "private, max-age=0";
                    oS.utilSetResponseBody("<html><body>Request for httpS://" + sSecureEndpointHostname + ":" + iPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());
                }
            };

            #endregion AttachEventListeners

            Fiddler.CONFIG.IgnoreServerCertErrors = false;
            FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);
            FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
            Fiddler.FiddlerApplication.Startup(iPort, oFCSF);

            oSecureEndpoint = FiddlerApplication.CreateProxyEndpoint(iPort, true, sSecureEndpointHostname);
            if (null != oSecureEndpoint)
            {
                FiddlerApplication.Log.LogFormat("Created secure endpoint listening on port {0}, using a HTTPS certificate for '{1}'", iPort, sSecureEndpointHostname);
            }
            bool bDone = false;
            while (!bDone)
            {

            }
        }  
    }
}


What is bad, Thanks avance.

Eric Lawrence

unread,
Dec 19, 2016, 1:27:07 PM12/19/16
to Fiddler
This line:
                oS.bBufferResponse = false;

means: "Send the response to the client before the BeforeResponse handler runs and has a chance to modify it." Set that to true instead, either inside the BeforeRequest method, or after examining the headers in the PeekAtResponseHeaders event handler.

jhon1...@hotmail.com

unread,
Dec 19, 2016, 1:59:56 PM12/19/16
to Fiddler


Thank Eric Lawrence , Now work but show the content in strange characters



U can help me.
 

Eric Lawrence

unread,
Dec 19, 2016, 2:03:09 PM12/19/16
to Fiddler
This:

 var oBody = System.Text.Encoding.UTF8.GetString(oS.responseBodyBytes);

Is not a reliable way to get the response body as a string. Instead, call oS.GetResponseBodyAsString(). Note that this API works on response bodies that are strings; binaries (e.g. images or applications) will be corrupted if you try to use this.

jhon1...@hotmail.com

unread,
Dec 19, 2016, 2:05:37 PM12/19/16
to Fiddler
I already tried but neither. :(

jhon1...@hotmail.com

unread,
Dec 19, 2016, 2:06:41 PM12/19/16
to Fiddler
                            oS.utilDecodeResponse();
                            string oBody = oS.GetResponseBodyAsString();
                            oBody += "<h1>RED<font color=red>proxy</font></h1>";
                            oS.utilSetResponseBody(oBody);


Eric Lawrence

unread,
Dec 19, 2016, 3:53:08 PM12/19/16
to Fiddler
I don't know what "I already tried but neither" means. 

  • How have you verified that FiddlerCore is capturing the request of interest?
  • What specifically happens?
Reply all
Reply to author
Forward
0 new messages