C# 如何将字节从循环存储到var,以便对web响应进行解压缩?

C# 如何将字节从循环存储到var,以便对web响应进行解压缩?,c#,arrays,byte,httpwebresponse,gzipstream,C#,Arrays,Byte,Httpwebresponse,Gzipstream,我一直在使用HTTPWebRequest/HTTPWebResponse构建自己的HTTP/1.0代理服务器。部分原始代码将响应从远程服务器直接传输到客户端,如下所示: if (response != null) { List<Tuple<String, String>> responseHeaders = ProcessResponse(response); StreamWriter myResponseWriter = new StreamWriter

我一直在使用HTTPWebRequest/HTTPWebResponse构建自己的HTTP/1.0代理服务器。部分原始代码将响应从远程服务器直接传输到客户端,如下所示:

if (response != null)
{
    List<Tuple<String, String>> responseHeaders = ProcessResponse(response);
    StreamWriter myResponseWriter = new StreamWriter(outStream);
    Stream responseStream = response.GetResponseStream();
    HttpStatusCode statusCode = response.StatusCode;
    string statusDesc = response.StatusDescription;

    Byte[] buffer;
    if (response.ContentLength > 0)
    {
        buffer = new Byte[response.ContentLength];
    }
    else
    {
        buffer = new Byte[BUFFER_SIZE];
    }

    int bytesRead;

    //send the response status and response headers
    WriteResponseStatus(statusCode, statusDesc, myResponseWriter);
    WriteResponseHeaders(myResponseWriter, responseHeaders);

    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        // this is the response body being streamed directly to the client browser
        outStream.Write(buffer, 0, bytesRead);
    }
}
我一直在尝试截取响应主体,以便修改内容,但在执行此操作之前,如果远程服务器已对内容进行了gzip/br/deflate,则需要对其进行解压缩。这是我到目前为止所想到的,但正如您从我的评论中所看到的,我无法确定如何将字节流存储到一个var中,以便发送它进行解压缩:

Byte[] buffer;
if (response.ContentLength > 0)
    buffer = new Byte[response.ContentLength];
else
    buffer = new Byte[BUFFER_SIZE];

int bytesRead;
var res = "";

// if the url and content type matches the criteria, then we want to edit it
if (hostPathMatch.Count > 0 && contentTypeMatch.Count > 0)
{
    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
    {   
        // how to we send this response stream to a var so that the entire contents can be sent to decompress

        //res += UTF8Encoding.UTF8.GetString(buffer, 0, bytesRead); // this doesnt work as it mangles gzipped contents
    }

    //was the page compressed? check the content-encoding header.
    if (responseHeaders.Any(p => p.Item1.ToLower() == "content-encoding" && p.Item2.ToLower() == "gzip"))
    {
        Output._Log.Output_Log("CONTENT IS GZIPPED");
        res = Tools.Unzip(res); // expects byte[], returns UTF8
    }

    // THIS IS WHERE WE WILL MODIFY THE BODY CONTENTS
    res = res.Replace("Foo", "Bar Bar");

    // then we will re-compress

    // update the response headers with the correct content length after modification
    responseHeaders.RemoveAll(p => p.Item1 == "Content-Length");
    responseHeaders.Add(new Tuple<string, string>("Content-Length", res.Length));

    //send the response status and response headers
    WriteResponseStatus(statusCode, statusDesc, myResponseWriter); 
    WriteResponseHeaders(myResponseWriter, responseHeaders);
}
else // we didnt want to modify this file, so just stream it out directly to the browser
{
    //send the response status and response headers
    WriteResponseStatus(statusCode, statusDesc, myResponseWriter);
    WriteResponseHeaders(myResponseWriter, responseHeaders);

    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        outStream.Write(buffer, 0, bytesRead);
    }
}

对于事先不知道总尺寸的情况,可以执行以下操作

[...]

List<byte> data = new List<byte>();
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    for(int i = 0; i < bytesRead; ++i)
        data.Add(buffer[i]);
}

var bytes = data.ToArray();

[...]
请注意,上面的代码段没有经过测试,必须添加一些检查,例如超出范围等。。此外,根据使用情况,它可能不适合您的需要,例如,对于海量数据


PS:不要忘记处理/清理所有IDisPobles。

如果您事先不知道完整的大小,您可以将收到的所有包保存在一个列表中。。。将它们合并到一个数组中,然后解压缩。否则,只需创建一个具有完整响应大小的缓冲区,并使用“Array.CopyTo”填充缓冲区。是否有可能提供一个示例?我明白你的意思,但我似乎无法理解如何将流读入任何其他类型的varRobert,我尝试了列表的第一种方法,因为我事先不知道总大小,它似乎不起作用。虽然如果我尝试UTF8Encoding.UTF8.GetStringbytes不会产生错误,但它只是空的。我已对我的答案进行了更改。请查看更改的数据。添加…,您可以尝试一下。在这种情况下,如果它仍然没有运行,我将尽快查看并测试代码段。
[...]

int offset = 0;
byte[] data = new byte[TOTAL_SIZE];
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    buffer.CopyTo(data, offset);
    offset += bytesRead;
}

[...]