简体   繁体   English

您如何在C#/ ASP.NET中读取HttpResponse对象的主体内容?

[英]how do you read the body content of HttpResponse object in C#/ ASP.NET?

How do you read the content of an HttpResponse object in C# / ASP.net? 您如何在C#/ ASP.net中读取HttpResponse对象的内容?

I need to be able to read the body content as a JSON object, modify it, and then write it back to the response output stream. 我需要能够将主体内容作为JSON对象读取,进行修改,然后将其写回到响应输出流。 I want to make sure I don't lose what's already in the stream, thus I need to read from it first. 我想确保我不会丢失流中已经存在的内容,因此我需要先阅读它。

How do I do this? 我该怎么做呢?

You can use a Delegating handler 您可以使用委托处理程序

public class ContentHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((responseToCompleteTask) =>
        {
            HttpResponseMessage response = responseToCompleteTask.Result;
            var YourContent = response.Content.ReadAsStreamAsync().Result;
            response.Content = new CompressedContent(response.Content, acceptedEncoding);
            return response;
        },
        TaskContinuationOptions.OnlyOnRanToCompletion);
    }
}

Register your handler at WebApiConfig 在WebApiConfig注册您的处理程序

GlobalConfiguration.Configuration.MessageHandlers.Add(new ContentHandler());

You can edit your reponse content by extending the HttpContent Class. 您可以通过扩展HttpContent类来编辑响应内容。 For example to compress content 例如压缩内容

    public class CompressedContent : HttpContent
{
    private HttpContent originalContent;

    protected override bool TryComputeLength(out long length)
    {
        length = -1;

        return false;
    }

    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        Stream editedStream = null;

        if (encodingType == "gzip")
        {
            editedStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true);
        }
        else if (encodingType == "deflate")
        {
            editedStream = new DeflateStream(stream, CompressionMode.Compress, leaveOpen: true);
        }

        return originalContent.CopyToAsync(editedStream).ContinueWith(tsk =>
        {
            if (editedStream != null)
            {
                editedStream.Dispose();
            }
        });
    }
}

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

相关问题 我如何阅读ASP.NET 2.0中的HttpResponse? - How do I read an HttpResponse in ASP.NET 2.0? 如何在ASP.NET C#中使用if(IsPostBack) - How do you use if (IsPostBack) in ASP.NET C# ASP.NET C#:可互换地写入HttpResponse或StringBuilder - ASP.NET C#: Write to HttpResponse or StringBuilder Interchangeably 如果您知道起始点和停止点但不知道 asp.net C# 中的中间点,如何拆分字符串 - How do you split a string if you know the start and stop point but not the middle in asp.net C# 如何使用AsyncFileUpload使用C#读取asp.net中的文件内容? - how to use AsyncFileUpload to read a file content in asp.net using c#? 如何配置SWFUpload与ASP.NET C#WebService一起使用? - How do you configure SWFUpload to work with an ASP.NET C# WebService? 如何使用AjaxFileUpload.js将HTML输入文件传递到C#ASP.Net? - How do you pass HTML input files to C# ASP.Net using AjaxFileUpload.js? 如何使用asp.net和c#流式传输Excel 2007或Word 2007文件 - How do you stream an Excel 2007 or Word 2007 file using asp.net and c# 如何从ASP.NET事件调用C#函数? - How do you call a C# function from an ASP.NET event? 如何使用 ASP.NET 内核制作 Razor 页面和 C# 代码框架? - How to do you make both a Razor Page and C# code framework with ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM