如何接受HttpResponse返回的二进制文件

如果要访问一个文件传输接口,当接口返回一个二进制的文件流应当如何接收:

using (Stream dataStream = response.GetResponseStream())
{
    int readCount = 0;

    //位运算,128k字节
    int bufferSize = 1 << 17;

    var buffer = new byte[bufferSize];
    using (var memory = new MemoryStream())
    {
        while ((readCount = dataStream.Read(buffer, 0, bufferSize)) > 0)
        {
            memory.Write(buffer, 0, readCount);
        }

        reportBytes = memory.ToArray();
     }
}

 

你可能感兴趣的:(.Net日常工作问题)