c# - 为什么 Request.InputStream 包含额外的字节?

标签 c# .net vb.net ashx

我正在将字节数组上传到 ASHX 处理程序

byte[] serializedRequest = SerializeRequest();

var uri = new Uri(_server.ActionUrl);
using (WebClient client = new WebClient())
{
    client.UploadData(uri, serializedRequest);
}

处理程序接收到

Dim str As Stream = context.Request.InputStream
Dim transformation(str.Length - 1) As Byte ' here I have extra "0"-byte
str.Position = 0
str.Read(transformation, 0, transformation.Length)

如您所见,我必须执行 str.Length - 1 来声明字节数组。这正在开发中。我什至不知道它在部署时会如何表现。这个字节从哪里来?这是一种可靠的方法还是我应该在流的开头添加一些字节以告知要从 Request.InputStream 读取多少字节?

最佳答案

Dim x(y) as Byte 实际上意味着“具有上限 y(长度 = y + 1)的数组”(Arrays in VB.NET)。

Dim transformation(str.Length) As Byte 实际上声明了比您需要的更大的数组,因此 str.Length - 1 语句是正确的。

确实没有 0 值字节,因为 Stream.Read() 不需要将流读取到末尾(检查方法返回值)并留下默认值 (0) 的额外字节.

关于c# - 为什么 Request.InputStream 包含额外的字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30578978/

相关文章:

c# - 如果未知变量发生变化,是否有解决单个未知数的通用方法?

c# - Enumerable.SingleOrDefault 方法返回的 "default value"是什么?

c# - 如何使 if 语句显示基于当前时间的 xml 属性

c# - EF 6 是否支持 SQL Server 2014 类型?

vb.net - 在某些单词 vb.net 之后或之前剥离字符串

c# - 协调工作线程关闭

mysql - asp.net 中带有文本和图像的自动完成文本框

c# - 我应该为对象和 MyType 覆盖 == 吗?

.net - xunit和System.Interactive.Async

c# - 灯光开关 : How do I place my business logic into a separate assembly?