【问题标题】:Capture raw HTTPS request and response with FiddlerCore使用 FiddlerCore 捕获原始 HTTPS 请求和响应
【发布时间】:2014-12-11 12:12:15
【问题描述】:

我有任务箱跟踪器,它将在 localhost 和 SOAP API 之间存储带有标头和正文的原始 HTTP 流量。我发现一个很好的解决方案是 FiddlerCore。应用程序仅适用于 HTTPS。目前我只收到了我的请求。但希望看到来自 SOAP API 的响应。

我在应用程序启动时初始化 FolderCore 代理:

        CONFIG.bCaptureCONNECT = true;
        CONFIG.IgnoreServerCertErrors = false;
        FiddlerApplication.Prefs.SetStringPref("fiddler.config.path.makecert", @"c:\Program Files (x86)\Fiddler2\Makecert.exe");
        FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
        if (!CertMaker.rootCertExists())
        {
            if (!CertMaker.createRootCert())
            {
                throw new Exception("Unable to create cert for FiddlerCore.");
            }
            X509Store certStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            certStore.Open(OpenFlags.ReadWrite);
            try
            {
                certStore.Add(CertMaker.GetRootCertificate());
            }
            finally
            {
                certStore.Close();
            }
        }

方法AfterSessionComplete:

    private void FiddlerApplication_AfterSessionComplete(Session sess)
    {
        // Ignore HTTPS connect requests
        if (sess.RequestMethod == "CONNECT")
        {
            return; 
        }

        if (sess == null || sess.oRequest == null || sess.oRequest.headers == null)
        {
            return; 
        }
        // Request
        string headers = sess.oRequest.headers.ToString();
        var reqBody = sess.GetRequestBodyAsString();

        // Response
        var resHeaders = sess.oResponse.headers.ToString();
        var resBody = sess.GetResponseBodyAsString();

        // if you wanted to capture the response
        //string respHeaders = session.oResponse.headers.ToString();
        //var respBody = session.GetResponseBodyAsString();

        // replace the HTTP line to inject full URL
        string firstLine = sess.RequestMethod + " " + sess.fullUrl + " " + sess.oRequest.headers.HTTPVersion;
        int at = headers.IndexOf("\r\n");
        if (at < 0)
            return;
        headers = firstLine + "\r\n" + headers.Substring(at + 1);

        string output = headers + "\r\n" +
                        (!string.IsNullOrEmpty(reqBody) ? reqBody + "\r\n" : string.Empty) + "\r\n\r\n";

    }

在 web.config 中添加:

<defaultProxy>
  <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
</defaultProxy>

【问题讨论】:

  • 您的标题指的是 HTTPS,但您的问题明确排除了 HTTPS。请您编辑您的标题以从 HTTPS 中删除 S。

标签: c# fiddler fiddlercore


【解决方案1】:

非常简单。为此使用了 sess.oResponse.headers 和 sess.GetResponseBodyAsString()

    var resHeaders = sess.oResponse.headers.ToString();
    var resBody = sess.GetResponseBodyAsString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 2014-02-24
    • 2018-06-24
    相关资源
    最近更新 更多