OWIN OnSendingHeaders回调 - 读响应体 [英] OWIN OnSendingHeaders Callback - Reading Response Body

查看:193
本文介绍了OWIN OnSendingHeaders回调 - 读响应体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是关系到优秀由优素福回答。我爱 OnSendingHeaders 回调。我现在可以添加响应头,无需担心转换流。不管怎么说,这里是我的问题。是否有可能读取响应主体回调里面,像这样。

 公众覆盖异步任务调用(OwinRequest要求,OwinResponse响应)
{
    request.OnSendingHeaders(州=>
        {
            VAR RESP =(OwinResponse)状态;
            //在这里,我想转换RESP,这是OwinResponse
            //为Htt的presponseMessage这样,当Content.ReadAsStringAsync
            //被取消,这Htt的presponseMessage对象,我想要的
            //响应体作为字符串。            VAR responseMessage =新的Htt presponseMessage();
            responseMessage.Content =新StreamContent(resp.Body);
            //这里我想打电话给
            // responseMessage.Content.ReadAsStringAsync()        },响应);
    等待Next.Invoke(请求,响应);}

我想从回调调用的方法是依赖于类部分的Htt presponseMessage ,并且不希望改变他们。

如果我设定的响应人体内存流之前流水线的处理开始(如最初由优素福在链接的答案建议),我能够得到这个工作。有没有更好的方式来在回调,而不是在这里做到这一点?

编辑:

这样行吗?

 公众覆盖异步任务调用(OwinRequest要求,OwinResponse响应)
{
    //使用要求的东西    流originalStream = response.Body;    VAR缓冲=新的MemoryStream();
    response.Body =缓冲;    等待Next.Invoke(请求,响应);    VAR responseMessage =新的Htt presponseMessage();    response.Body.Seek(0,SeekOrigin.Begin);    responseMessage.Content =新StreamContent(response.Body);    //通行证responseMessage其他类的
    要读取//响应体这样的
    // responseMessage.Content.ReadAsStringAsyn()
    //添加更多的响应头    如果(缓冲= NULL&放大器;!&安培; buffer.Length大于0)
    {
        buffer.Seek(0,SeekOrigin.Begin);
        等待buffer.CopyToAsync(originalStream);
    }
}


解决方案

你怎么想响应主体办?

这调用回调函数第一次写,所以这是来不及更换流。还因为其中没有存储在它通常不能从反应流中读取。这通常是出去的网络只写流。

在这里更换响应流前面是正确的做法。

This question is related to the excellent answer by Youssef. I love OnSendingHeaders callback. I can now add the response headers without worrying about switching streams. Anyways, here is my question. Is it possible to read the response body inside the callback, like so.

public override async Task Invoke(OwinRequest request, OwinResponse response)
{
    request.OnSendingHeaders(state =>
        {
            var resp = (OwinResponse)state;
            // Here, I want to convert resp, which is OwinResponse
            // to HttpResponseMessage so that when Content.ReadAsStringAsync
            // is called off this HttpResponseMessage object, I want the 
            // response body as string.

            var responseMessage = new HttpResponseMessage();
            responseMessage.Content = new StreamContent(resp.Body);
            // Here I would like to call
            // responseMessage.Content.ReadAsStringAsync()

        }, response);


    await Next.Invoke(request, response);

}

The methods I want to call from the callback are part of classes that depend on HttpResponseMessage and do not want to change them.

If I set the response body to memory stream before the pipeline processing starts (as was initially suggested by Youssef in the linked answer), I'm able to get this working. Is there a better way to do this here in the callback instead of that?

EDIT:

Is this okay?

public override async Task Invoke(OwinRequest request, OwinResponse response)
{
    // Do something with request

    Stream originalStream = response.Body;

    var buffer = new MemoryStream();
    response.Body = buffer;

    await Next.Invoke(request, response);

    var responseMessage = new HttpResponseMessage();

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

    responseMessage.Content = new StreamContent(response.Body);

    // Pass responseMessage to other classes for the
    // response body to be read like  this
    // responseMessage.Content.ReadAsStringAsyn()
    // Add more response headers

    if (buffer != null && buffer.Length > 0)
    {
        buffer.Seek(0, SeekOrigin.Begin);
        await buffer.CopyToAsync(originalStream);
    }
}

解决方案

What do you want to do with the response body?

This callback is invoked on first write, so it's too late to replace the stream. You also can't read from the response stream as there is nothing stored in it normally. This is normally a write-only stream that goes out to the network.

Replacing the response stream earlier is the correct approach here.

这篇关于OWIN OnSendingHeaders回调 - 读响应体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆