【问题标题】:Logging request length using IHttpModule使用 IHttpModule 记录请求长度
【发布时间】:2014-06-23 14:18:31
【问题描述】:

我制作了一个记录 Web 请求的 IHttpModule。它记录 POST 请求就好了。但是对于 GET 请求,长度似乎总是为零。有没有办法获取 GET 请求的长度?

    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
            string url = app.Request.RequestContext.HttpContext.Request.Url.ToString();
            string requestType = app.Request.RequestContext.HttpContext.Request.RequestType;
            long requestLength = app.Request.InputStream.Length;
            Log(url, requestType, requestLength);
    }

【问题讨论】:

    标签: c# asp.net ihttpmodule


    【解决方案1】:

    GET 请求通常没有消息正文,因此,Content-Length 将为零。

    【讨论】:

    • 感谢 Rodrick,但我如何获得整个请求的长度? (所有 HTTP 标头内容)
    • @Fidel 我现在无法对此进行测试,但您可以尝试直接迭代服务器变量。我会从 ALL_RAW 开始。
    • 感谢 Rodrick,是的,ServerVariables["ALL_RAW"] 非常接近。但是它不包含 HTTP 请求的第一行。
    • @Fidel:第一行就是GET <AbsolutePathAndQuery> HTTP/1.1(动词可以从Request.HttpMethod获取,路径/查询可以从Request.Url.PathAndQuery获取)
    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2021-05-02
    相关资源
    最近更新 更多