使用StreamReader读取HttpContent流,直到字符限制 [英] Read HttpContent stream until a character limit using StreamReader

查看:423
本文介绍了使用StreamReader读取HttpContent流,直到字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下代码(该代码将HttpContent的完整字符串响应读取)转换为字符串,以仅读取一定数量的最大字符.现有代码:

I am trying to convert the following code that reads the complete string response of a HttpContent into a string, to read only a certain maximum number of characters. Existing code:

private static async Task<string> GetContentStringAsync(HttpContent content)
{
    string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false);
    return responseContent;
}

我现在拥有的代码:

private static async Task<string> GetContentStringAsync(HttpContent content, int ResponseContentMaxLength)
{
    string responseContent;
    Stream responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
    using (StreamReader streamReader = new StreamReader(responseStream))
    {
        // responseContent = Data from streamReader until ResponseContentMaxLength
    }

    return responseContent;
}

我是StreamReader和HttpContent操作的新手.有没有办法做到这一点?

I am new to StreamReader and HttpContent manipulation. Is there a way to do this?

推荐答案

有多种方法可以做到这一点.但是,恕我直言,最简单的方法之一是创建一个MemoryStream,您已经在其中读取了所需的确切字节数,然后从该流中读取了StreamReader对象,而不是原来的对象.

There are a variety of ways to do this. However, IMHO one of the simplest is to create a MemoryStream into which you've read the exact number of bytes you want, and then have the StreamReader object read from that stream instead of the original one.

例如:

private static async Task<string> GetContentStringAsync(HttpContent content, int ResponseContentMaxLength)
{
    string responseContent;
    Stream responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

    int totalBytesRead = 0;
    byte[] buffer = new byte[ResponseContentMaxLength];

    while (totalBytesRead < buffer.Length)
    {
        int bytesRead = await responseStream
            .ReadAsync(buffer, totalBytesRead, buffer.Length - totalBytesRead);

        if (bytesRead == 0)
        {
            // end-of-stream...can't read any more
            break;
        }

        totalBytesRead += bytesRead;
    }

    MemoryStream tempStream = new MemoryStream(buffer, 0, totalBytesRead);

    using (StreamReader streamReader = new StreamReader(tempStream))
    {
        // responseContent = Data from streamReader until ResponseContentMaxLength
    }

    return responseContent;
}

当然,以上假设ResponseContentMaxLength的值足够小,以至于合理分配足够大的byte[]来临时存储这么多字节是合理的.由于返回的内容将具有可比较的规模,因此这似乎是一个合理的假设.

The above of course assumes that ResponseContentMaxLength has a value small enough that it is reasonable to allocate a byte[] large enough to temporarily store that many bytes. Since the content returned is going to be of comparable scale, this seems like a reasonable assumption.

但是,如果您不想维护该额外的缓冲区,则另一种方法是编写一个Stream类,该类仅从您指定的字节数中读取底层流对象,然后传递该实例的一个实例. (使用ResponseContentMaxLength值初始化)到StreamReader对象.与上述相比,这需要大量的额外工作. (尽管如此,我想因为这是一个有用的对象,所以可能已经有一个公开可用的实现了.我知道我自己至少写过几次类似的东西,但我只是碰巧没有方便使用的代码此刻).

But if you don't want to maintain that extra buffer, an alternative approach would be to write a Stream class that reads from an underlying stream object only as many bytes as you specify, and then pass an instance of that (initialized with the ResponseContentMaxLength value) to the StreamReader object. That's quite a lot of extra work as compared to the above though. (Though, I suppose since that's such a useful object, there might be a publicly available implementation already…I know I've written something like that at least a couple of times myself, I just don't happen to have the code handy at the moment).

这篇关于使用StreamReader读取HttpContent流,直到字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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