Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从基本流读取(httpRequestStream)_C#_.net_Http_Stream - Fatal编程技术网

C# 从基本流读取(httpRequestStream)

C# 从基本流读取(httpRequestStream),c#,.net,http,stream,C#,.net,Http,Stream,我有一个基本流,它是HTTP请求流 及 我想读取流(其中包含非字符内容,因为我已经发送了数据包) 当我们用StreamReader包装这个流时,我们使用StreamReader的ReadToEnd()函数,它可以读取整个流并返回字符串 HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://127.0.0.1/"); listener.Start(); var context = listener.Get

我有一个基本流,它是HTTP请求流 及

我想读取流(其中包含非字符内容,因为我已经发送了数据包)

当我们用StreamReader包装这个流时,我们使用StreamReader的ReadToEnd()函数,它可以读取整个流并返回字符串

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://127.0.0.1/");
listener.Start();
var context = listener.GetContext();
var sr = new StreamReader(context.Request.InputStream);
string x=sr.ReadToEnd(); //This Workds
但是由于它有非字符内容,我们不能使用StremReader(我尝试了所有的编码机制..使用字符串是错误的)。我不能使用这个函数

context.Request.InputStream.Read(buffer,position,Len) 
因为我不能得到流的长度,InputStream.length总是抛出一个异常,不能使用..我不想创建一个像[size][file]这样的小协议,先读取大小然后再读取文件..不知何故StreamReader可以得到长度..我只是想知道如何。 我也试过了,但没用

List<byte> bb = new List<byte>();
var ss = context.Request.InputStream;
byte b = (byte)ss.ReadByte();
while (b >= 0)
{
    bb.Add(b);
    b = (byte)ss.ReadByte();
}

谢谢Jon,Douglas

StreamReader
也无法获得长度——关于的第三个参数,似乎有些混乱。该参数指定将被读取的最大字节数,它不需要(实际上不能)等于流中实际可用的字节数。您只需在循环中调用
Read
,直到它返回
0
,在这种情况下,您就知道您已经到达了流的末尾。所有这些都记录在MSDN上,这也正是
StreamReader
如何做到的


使用
StreamReader
读取请求并将其放入
字符串中也没有问题;字符串在.NET中是二进制安全的,所以您可以使用它。问题是如何理解字符串的内容,但我们不能真正讨论这个问题,因为您没有提供任何相关信息。

您的错误在于以下几行:

byte b = (byte)ss.ReadByte();
字节
类型是无符号的;当在流的末尾返回-1时,您不分青红皂白地将其强制转换为
字节
,该字节将其转换为255,因此满足
b>=0
条件。注意返回类型是
int
,而不是
byte
,这一点很有帮助,因为正是这个原因

针对您的代码的快速修复程序:

List<byte> bb = new List<byte>();
var ss = context.Request.InputStream;
int next = ss.ReadByte();
while (next != -1)
{
    bb.Add((byte)next);
    next = ss.ReadByte();
}

HttpRequestStream
不会给出长度,但您可以从
HttpListenerRequest.ContentLength64
属性获取长度。正如Jon所说,确保观察
Read
方法的返回值。在我的例子中,我们得到缓冲读取,无法一次性读取整个226KB的有效负载

试一试

byte[]getPayload(HttpListenerContext上下文)
{
int length=(int)context.Request.ContentLength64;
字节[]有效载荷=新字节[长度];
int numRead=0;
while(numRead<长度)
numRead+=context.Request.InputStream.Read(有效负载,numRead,长度-numRead);
返回有效载荷;
}

True..我没有解释太多..len的内容是一个已经创建的字节[]b=新字节[len],但这个len我无法获得..我的字符串问题是我们必须以字节到字符串的两种方式使用编码,反之亦然..但在编码步骤中总是弄乱我的字节..我的tried var sr=新的StreamReader(context.Request.InputStream,Encoding.ASCII);然后unicode和UTF8使用“New-ascienceoding().GetBytes(str)”我得到了经过筛选的字节..ReadByte函数在流的末尾返回-1..仍然得到无限循环..@CnativeFreak:您不能也不需要获得长度。选择您自己的缓冲区大小并重复读取。此外,您不能使用ASCII以外的任何编码,因为这会弄乱您的输入数据。但您仍然没有提供ny关于您实际尝试执行的操作的信息。Lol,我刚刚看到您的评论。。感谢您提供的信息…但我将使用该文件作为第二个数组。。因此我不需要调整数组的大小。我假设您的上一个版本应该可以工作。两个提示:(1)学会使用“using”语句自动关闭文件流。(2)您可能不需要这么大的byte[]缓冲区,因为您不太可能在任何一次读取调用中读取那么多数据。大约8192的大小可能就足够了。
byte b = (byte)ss.ReadByte();
List<byte> bb = new List<byte>();
var ss = context.Request.InputStream;
int next = ss.ReadByte();
while (next != -1)
{
    bb.Add((byte)next);
    next = ss.ReadByte();
}
var ss = context.Request.InputStream;

byte[] buffer = new byte[1024];
int totalCount = 0;

while (true)
{
    int currentCount = ss.Read(buffer, totalCount, buffer.Length - totalCount);
    if (currentCount == 0)
        break;

    totalCount += currentCount;
    if (totalCount == buffer.Length)
        Array.Resize(ref buffer, buffer.Length * 2);
}

Array.Resize(ref buffer, totalCount);
    byte[] getPayload(HttpListenerContext context)
    {
        int length = (int)context.Request.ContentLength64;
        byte[] payload = new byte[length];
        int numRead = 0;
        while (numRead < length)
            numRead += context.Request.InputStream.Read(payload, numRead, length - numRead);

        return payload;
    }