相关疑难解决方法(0)

如何使用HttpModule记录请求输入流,然后重置InputStream位置

我正在尝试使用像这样的IHttpModule记录http请求的内容:

public class LoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
        var request = ((HttpApplication)sender).Request;
        string content;

        using (var reader = new StreamReader(request.InputStream))
        {
            content = reader.ReadToEnd();
        }

        LogRequest(content)
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,在将输入流读取到结尾后,InputStream似乎已消失或更有可能,光标位于流的末尾.

我尝试过request.InputStream.Position = 0;,request.InputStream.Seek(0, SeekOrigin.Begin);但都没有工作.

.net inputstream httpmodule

31
推荐指数
2
解决办法
2万
查看次数

如何获取已读取的内容

我有一个继承自ApiController的类.它有一个像这样的Put方法:

[PUT("user/{UserId}")]
public HttpResponseMessage Put(string userId, PaymentRequest paymentRequest)
{
    // Calling business logic and so forth here
    // Return proper HttpResponseMessage here
}
Run Code Online (Sandbox Code Playgroud)

该方法在上面工作正常.现在我需要验证方法调用的签名,但在这里我遇到了一个问题.签名本质上是方法+ url + body的组合.我可以通过调用Request.Method和我可以通过调用Request.RequestUri.ToString()得到的url获得的方法,但是我无法得到它之前的身体,因为它被自动反序列化为PaymentRequest对象之前 asp.net MVC4框架.

我的第一次尝试: 因为我现在已经理解了Request.Content.ReadAsStringAsync().结果什么也没有返回.这是因为内容只能读取一次.

我的第二次尝试: 我尝试将其序列化为JSON字符串.

var serializer = new JavaScriptSerializer();
var paymentRequestAsJson = serializer.Serialize(paymentRequest);
Run Code Online (Sandbox Code Playgroud)

这个问题是格式化与签名的正文部分略有不同.它具有相同的数据,但有一些空格.

我无法改变Put-method的调用者所做的事情,因为这是第三方组件.我该怎么办?

c# asp.net asp.net-web-api

19
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net ×1

asp.net ×1

asp.net-web-api ×1

c# ×1

httpmodule ×1

inputstream ×1