【问题标题】:Parsing POST request data with FiddlerCore使用 FiddlerCore 解析 POST 请求数据
【发布时间】:2014-02-27 13:50:08
【问题描述】:

我正在尝试捕获本地 POST 请求并解析其数据。
仅用于测试,FiddlerCore 应该只响应它已解析的数据。
下面是 FidlerCore 封装的代码:

private void FiddlerApplication_BeforeRequest(Session oSession)
{
    if (oSession.hostname != "localhost") return;

    eventLog.WriteEntry("Handling local request...");
    oSession.bBufferResponse = true;
    oSession.utilCreateResponseAndBypassServer();
    oSession.oResponse.headers.HTTPResponseStatus = "200 Ok";
    oSession.oResponse["Content-Type"] = "text/html; charset=UTF-8";
    oSession.oResponse["Cache-Control"] = "private, max-age=0";

    string body = oSession.GetRequestBodyAsString();
    oSession.utilSetResponseBody(body);
}

这是请求发送者的代码:

const string postData = "This is a test that posts this string to a Web server.";

try
{
    WebRequest request = WebRequest.Create("http://localhost/?action=print");
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentLength = byteArray.Length;
    request.ContentType = "text/html";
    request.Method = "POST";

    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(byteArray, 0, byteArray.Length);
    }

    using (WebResponse response = request.GetResponse())
    {
        txtResponse.Text = ((HttpWebResponse)response).StatusDescription;
        using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader streamReader = new StreamReader(stream))
            {
                string responseFromServer = streamReader.ReadToEnd();
                streamReader.Close();
                txtResponse.Text = responseFromServer;
            }
        }
    }
}
catch (Exception ex)
{
    txtResponse.Text = ex.Message;
}

我收到以下错误:

服务器违反了协议。 Section=ResponseStatusLine

我做错了什么?

【问题讨论】:

  • 你知道如何在 Fiddler.Session 对象中获取 Request Payload 吗?

标签: c# proxy fiddler fiddlercore


【解决方案1】:

通过改变让它工作:

WebRequest request = WebRequest.Create("http://localhost/?action=print");

WebRequest request = WebRequest.Create("http://localhost:8877/?action=print");

从浏览器调用此 URL 会被 FiddlerCore 正确拦截,而无需指定端口号。我不认为我应该插入监听端口,因为 FiddlerCore 应该拦截所有流量,对吧?

【讨论】:

  • Fiddler 捕获客户端设置为将其用作代理的所有请求。 .NET 应用程序可能会或可能不会采用系统的代理设置(您甚至没有显示您是否已将 FiddlerCore 配置为注册为系统代理)。通过指定端口,您可以有效地将 FiddlerCore 直接用作反向代理。
  • 我确实像这样将 FiddlerCore 注册为系统代理 - FiddlerApplication.Startup(8877, true, true); 但由于它托管在 Windows 服务中,因此它的行为有所不同。启动服务时,它不会像托管在 Windows 应用程序中那样设置浏览器的代理设置。因此,当尝试调用本地方法时,为了处理服务,我注意到我必须包含端口号,即使从像这样的浏览器调用它 - http://localhost:8877/?Action=Print&PrinterID=Printer1
  • @EricLaw 我想我明白发生了什么 - 拥有 FiddlerCore 的服务正在使用 LocalSystem 帐户并使用此帐户注册 FiddlerCore,但用户使用的是自己的帐户。如何使用用户帐户而不是服务帐户注册 FiddlerCore?
  • 要么在用户帐户中运行 FiddlerCore,要么模拟用户并将其代理设置为 127.0.0.1:8888。
  • 我找到的一个解决方案是使用您自己的 SetProxy 应用程序。我在设置服务时运行它并让它设置代理设置。您能否提供如何模拟用户凭据的代码示例?
猜你喜欢
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多