无法读取内存流c#

时间:2016-09-20 12:38:17

标签: c# exception stream streamreader memorystream

我正在使用一些数据,这是一个mvc api。在这种情况下,我使用邮递员传递数据。 数据格式是原始文本。我传递给API的值如下,并代表EDF(http://www.edfplus.info/)数据:

  "rawdata":{

 "binaryencoding":"base64","FileID":3,"ExtensionName":"edf",

   "value":"gMnW5\/P5  \/Pz16+HZ087KyMbEwsHBwMDAwMHBwcLCw8PExMXFxsfHyMnJysvLy8zMy8vLysrKysnJycnJycrKysvLy8vMzMzMzc7Q0dLS0dHQzs3MysjHxcTCwL69u7m3trSysbCvra2sq6uqqqqqqqqqq6urrKysrK2tra.....QjY2SnKu7zNnb1Mi7sKehnJiWlJKRkZCQkJCRkZGSkpOTk5SUlJWVlpaXl5iYmZmampqam5qbm5ubm5ycnJ2dnp6fn6CgoaGioqGhoKCfnp6dnJybm5uampqamZmZmZiYmJeXlpaWlZWVlZSUlJOTk5KSkZGRkZGQkJCQkI+Pj4+Pj4+Pj..."

这里你是一些代码。为了简单起见,我将把httppost中的rawdata.value直接分配给之前创建的对象,Value是一个字符串。

myObject.value=rawdata.value;
byte[] data=Base64.Convert.FromBase64String(myObject.value);(myobject.Value); //this gives you a 15.000 positions array.
Stream stream = new MemoryStream(data);
//the stream gets those ReadTimeOut and WriteTimeOut exceptions,after being created
StreamReader Sr=new StreamReader(stream);
char[] header=new char[256];
sr.ReadBlock(header, 0, 256);

当您检查块时,它具有如下所示的所有值,带有 符号:

[0] = 65533 '�'

我还要补充一点,当我检查Stream时,我可以通过以下错误看到流readtimeout和writetimeout属性:System.InvalidOperationException

你知道为什么会出现这个错误吗?感谢

1 个答案:

答案 0 :(得分:-1)

创建MemoryStream后问题可能是帽子,位置指向数组的末尾。你试过这个吗?

// this value comes from raw data, and for this example, lets suppose that it is all right.
byte[] data=Base64.bDecodingForString(Value);
Stream stream = new MemoryStream(data);
//the stream gets those ReadTimeOut and WriteTimeOut exceptions,after being created
stream.Position = 0;
if(stream.CanRead) 
{
   StreamReader Sr=new StreamReader(stream);
   char[] header=new char[256];
   sr.ReadBlock(header, 0, 256);
}
else 
{
   //Show error
}