【问题标题】:How can i capture https respond via fiddlercore?如何通过 fiddlercore 捕获 https 响应?
【发布时间】:2017-03-10 11:18:41
【问题描述】:

我已经下载了 fiddlercore DEMO,我尝试在我的应用程序中使用它,这取决于 WPF,然后我有一个问题,在我的应用程序中我无法捕获 https 响应资源? enter image description here

我的代码就在下面:

    using Fiddler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;

namespace operateToolWPF.Utils
{
    public class MyFiddler
    {
        static Proxy oSecureEndpoint;
        static string sSecureEndpointHostname = "localhost";
        static int iSecureEndpointPort = 7777;
        public List<Fiddler.Session> oAllSessions { get; set; }


        public  void DoQuit()
        {
            if (null != oSecureEndpoint) oSecureEndpoint.Dispose();
            Fiddler.FiddlerApplication.Shutdown();
            //Thread.Sleep(500);
        }
        private  string CalcResponseSize(Session oS)
        {
            if (null == oS.oResponse) return String.Empty;
            var cBytesOut = 0;

            if (null != oS.responseBodyBytes) cBytesOut += oS.responseBodyBytes.Length;
            if ((null != oS.oResponse) && (null != oS.oResponse.headers)) cBytesOut +=
               oS.oResponse.headers.ByteCount();
            return cBytesOut.ToString();
        }

        public  void WriteSessionList(List<Fiddler.Session> oAllSessions)
        {
            ConsoleColor oldColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Session list contains...");
            try
            {
                Monitor.Enter(oAllSessions);
                foreach (Session oS in oAllSessions)
                {
                    Console.Write(String.Format("{0} {1} {2} {3} {4}  {5}  {6}\n", oS.id, oS.oRequest.headers.HTTPMethod, oS.fullUrl, oS.responseCode, oS.oResponse.MIMEType, (oS.Timers.ClientBeginResponse - oS.Timers.ClientBeginRequest), CalcResponseSize(oS)));
                }
            }
            finally
            {
                Monitor.Exit(oAllSessions);
            }
            Console.WriteLine();
            Console.ForegroundColor = oldColor;
        }


        public void DoFiddler()
        {
            oAllSessions = new List<Fiddler.Session>();
            //if (!Fiddler.CertMaker.rootCertExists())
            //{
            //    if (!Fiddler.CertMaker.createRootCert())
            //    {
            //        throw new Exception("Unable to create cert for FiddlerCore.");
            //    }
            //}

            //if (!Fiddler.CertMaker.rootCertIsTrusted())
            //{
            //    if (!Fiddler.CertMaker.trustRootCert())
            //    {
            //        throw new Exception("Unable to install FiddlerCore's cert.");
            //    }
            //}

            #region AttachEventListeners
            Fiddler.FiddlerApplication.OnNotification += delegate(object sender, NotificationEventArgs oNEA) { 
                Console.WriteLine("** NotifyUser: " + oNEA.NotifyString); 
            };
            Fiddler.FiddlerApplication.Log.OnLogString += delegate(object sender, LogEventArgs oLEA) { 
                Console.WriteLine("** LogString: " + oLEA.LogString); 
            };

            Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
            {
                //Console.WriteLine("Fiddler.FiddlerApplication.BeforeRequest");
                oS.bBufferResponse = true;
                Monitor.Enter(oAllSessions); 
                oAllSessions.Add(oS);
                //Console.Write(String.Format("{0} {1} {2} {3} {4}  {5}  {6}\n", oS.id, oS.oRequest.headers.HTTPMethod, oS.fullUrl, oS.responseCode, oS.oResponse.MIMEType, (oS.Timers.ClientBeginResponse - oS.Timers.ClientBeginRequest), CalcResponseSize(oS)));
                Monitor.Exit(oAllSessions);
                oS["X-AutoAuth"] = "(default)";
                if ((oS.oRequest.pipeClient.LocalPort == iSecureEndpointPort) && (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 + ":" + iSecureEndpointPort.ToString() + " received. Your request was:<br /><plaintext>" + oS.oRequest.headers.ToString());
                }
            };

            Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)
            {
                //Console.WriteLine("Fiddler.FiddlerApplication.AfterSessionComplete");
                //Console.Title = ("Session list contains: " + oAllSessions.Count.ToString() + " sessions");
                //DoQuit();
            };

            // Tell the system console to handle CTRL+C by calling our method that
            // gracefully shuts down the FiddlerCore.
            //
            // Note, this doesn't handle the case where the user closes the window with the close button.
            // See http://geekswithblogs.net/mrnat/archive/2004/09/23/11594.aspx for info on that...
            //
            #endregion AttachEventListeners
            string sSAZInfo = "NoSAZ";
#if SAZ_SUPPORT
            sSAZInfo = Assembly.GetAssembly(typeof(Ionic.Zip.ZipFile)).FullName;

            DNZSAZProvider.fnObtainPwd = () =>
            {
                Console.WriteLine("Enter the password (or just hit Enter to cancel):");
                string sResult = Console.ReadLine();
                Console.WriteLine();
                return sResult;
            };

            FiddlerApplication.oSAZProvider = new DNZSAZProvider();
#endif

            Console.WriteLine(String.Format("Starting {0} ({1})...", Fiddler.FiddlerApplication.GetVersionString(), sSAZInfo));
            Fiddler.CONFIG.IgnoreServerCertErrors = false;
            FiddlerApplication.Prefs.SetBoolPref("fiddler.network.streaming.abortifclientaborts", true);

            FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
            int iPort = 8877;
            Fiddler.FiddlerApplication.Startup(iPort, oFCSF);
            FiddlerApplication.Log.LogFormat("Created endpoint listening on port {0}", iPort);

            FiddlerApplication.Log.LogFormat("Starting with settings: [{0}]", oFCSF);
            FiddlerApplication.Log.LogFormat("Gateway: {0}", CONFIG.UpstreamGateway.ToString());

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


        }
    }
}

谁能帮助我?谢谢!

【问题讨论】:

    标签: c# wpf https fiddlercore


    【解决方案1】:

    我希望这能解决这个 Demo 应用程序中的所有问题,他已经清楚地解释了如何跟踪 HTTP 和 HTTPS 请求。对于 HTTP,您可以直接监听 AfterSessionComplete 事件,但对于 HTTPS,您需要安装 Fiddler 核心证书这 https://weblog.west-wind.com/posts/2014/jul/29/using-fiddlercore-to-capture-http-requests-with-net

    【讨论】:

    • 非常感谢!你的进步对我有用!虽然太长了很多都看不懂!
    • 如果您对提琴手核心有任何疑问,您可以问...!!或者如果您发现任何困难。
    • 我有一个新问题。如何隐藏安装证书(DO_NOT_TRUST_FiddlerRoot)的通知窗口?这是关于用户体验的。谢谢!
    • 您无法避免 DO_NOT_TRUST_FiddlerRoot 窗口您可以做的一件事是您可以在本地文件中维护证书及其密钥,并且当您重新启动服务时,只需从这样就不会弹出窗口了..看看如何保存密钥和证书。
    • @zhangmike 是否对您有效,如果有效,然后将其标记为答案并进行投票,以便对其他用户有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多