简体   繁体   English

如何在 .net core 中获取 HttpRequest 主体?

[英]How to get HttpRequest body in .net core?

I want to get Http Request body in .net core , I used this code:我想在 .net core 中获取 Http 请求正文,我使用了以下代码:

using (var reader
    = new StreamReader(req.Body, Encoding.UTF8))
{
    bodyStr = reader.ReadToEnd();
}
req.Body.Position = 0

But I got this error:但我收到了这个错误:

System.ObjectDisposedException: Cannot access a disposed object. System.ObjectDisposedException:无法访问已处理的对象。 Object name: 'FileBufferingReadStream'.对象名称:'FileBufferingReadStream'。

An error occurs after the using statement of the line该行的 using 语句后出现错误

How to get HttpRequest Body in .net core?如何在 .net core 中获取 HttpRequest Body? and how to fix this error?以及如何解决这个错误?

use this extension method to get httpRequest Body:使用此扩展方法获取 httpRequest Body:

   public static string GetRawBodyString(this HttpContext httpContext, Encoding encoding)
    {
        var body = "";
        if (httpContext.Request.ContentLength == null || !(httpContext.Request.ContentLength > 0) ||
            !httpContext.Request.Body.CanSeek) return body;
        httpContext.Request.EnableRewind();
        httpContext.Request.Body.Seek(0, SeekOrigin.Begin);
        using (var reader = new StreamReader(httpContext.Request.Body, encoding, true, 1024, true))
        {
            body = reader.ReadToEnd();
        }
        httpContext.Request.Body.Position = 0;
        return body;
    }

The important thing is that HttpRequest.Body is a Stream type And when the StreamReader is disposed, HttpRequest.Body is also disposed.重要的是 HttpRequest.Body 是一个 Stream 类型,并且当 StreamReader 被释放时,HttpRequest.Body 也会被释放。

I had this problem until I found the below link in GitHub: Refer to the below link and the GetBody method https://github.com/devdigital/IdentityServer4TestServer/blob/3eaf72f9e1f7086b5cfacb5ecc8b1854ad3c496c/Source/IdentityServer4TestServer/Token/TokenCreationMiddleware.cs我遇到了这个问题,直到我在 GitHub 中找到以下链接:请参阅以下链接和 GetBody 方法https://github.com/devdigital/IdentityServer4TestServer/blob/3eaf72f9e1f7086b5cfacb5ecc8b1854ad3c496c/Source/IdentityServer4TestServer/Token/TokenCre

Simple workaround:简单的解决方法:

using (var content = new StreamContent(Request.Body))
{
     var contentString = await content.ReadAsStringAsync();
}

The accepted answer did not work for me, but I was reading the body twice.接受的答案对我不起作用,但我读了两次正文。

    public static string ReadRequestBody(this HttpRequest request, Encoding encoding)
    {
        var body = "";
        request.EnableRewind();

        if (request.ContentLength == null ||
            !(request.ContentLength > 0) ||
            !request.Body.CanSeek)
        {
            return body;
        }

        request.Body.Seek(0, SeekOrigin.Begin);

        using (var reader = new StreamReader(request.Body, encoding, true, 1024, true))
        {
            body = reader.ReadToEnd();
        }

        //Reset the stream so data is not lost
        request.Body.Position = 0;

        return body;
    }

Please review answer by Stephen Wilkinson请查看斯蒂芬威尔金森的回答

Use the following for .NET Core 3.1将以下内容用于.NET Core 3.1

Startup.cs启动文件

app.Use((context, next) =>
{
    context.Request.EnableBuffering(); // calls EnableRewind() `https://github.com/dotnet/aspnetcore/blob/4ef204e13b88c0734e0e94a1cc4c0ef05f40849e/src/Http/Http/src/Extensions/HttpRequestRewindExtensions.cs#L23`
    return next();
});

You should then be able to rewind as per other answers:然后,您应该能够按照其他答案倒带:

httpContext.Request.Body.Seek(0, SeekOrigin.Begin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM