为什么 Request.InputStream 包含额外的字节?
Why Request.InputStream contains extra byte?
我正在将字节数组上传到 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
其实就是"array with upper bound y (length = y + 1)"(Arrays in VB.NET).
Dim transformation(str.Length) As Byte
实际上声明了比你需要的更大的数组,所以 str.Length - 1
声明是正确的。
确实没有 0 值字节,因为 Stream.Read()
不需要将流读取到末尾(检查方法 return 值)并将额外的字节保留为默认 (0) 值。
我正在将字节数组上传到 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
其实就是"array with upper bound y (length = y + 1)"(Arrays in VB.NET).
Dim transformation(str.Length) As Byte
实际上声明了比你需要的更大的数组,所以 str.Length - 1
声明是正确的。
确实没有 0 值字节,因为 Stream.Read()
不需要将流读取到末尾(检查方法 return 值)并将额外的字节保留为默认 (0) 值。