English 中文(简体)
阅读基本版本(http://questStream)
原标题:Read from basic stream (httpRequestStream)

I have a basic stream which is the stream of HTTP request and

var s=new HttpListener().GetContext().Request.InputStream;

我想读一下溪流(包含非骚扰内容,因为一把包装单寄出)

当我们用精简程序来总结这一流时,我们利用“Reenader”的阅读功能,可以读到整个流,并回击......

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) 

because I cant get the length of the stream, InputStream.Length always throws an exception and cant be used..and i dont want to create a small protocol like [size][file] and read first size then the file ...somehow the StreamReader can get the length ..and i just want to know how . I also tried this and it didn t work

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();
}

我以以下方式解决了这一问题:

FileStream fs = new FileStream("C:\cygwin\home\Dff.rar", FileMode.Create);
byte[] file = new byte[1024 * 1024];
int finishedBytes = ss.Read(file, 0, file.Length);
while (finishedBytes > 0)
{
    fs.Write(file, 0, finishedBytes);
    finishedBytes = ss.Read(file, 0, file.Length);
}
fs.Close();

thanks Jon , Douglas

最佳回答

你的 b子如下:

byte b = (byte)ss.ReadByte();

Stream.ReadByte 页: 1 值得注意的是,由于这一原因,回归类型为<代码>int,而不是byte

your:

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();
}

以下解决办法效率更高,因为它避免了<代码>ReadByte电话引起的逐字逐句,而是在<代码>Read上使用动态扩展的散列阵列。 内部实施:

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);
问题回答

:Stream.Read 。 该参数具体指明了将读到的maximum份数,而不需要(而且实际上无法做到)的份数与流中实际可用的份数相等。 您仅打电话Read,直至其填满为止。 这在MSDN上都是有记录的,而且确切地说,它是如何做到<代码>StreamReader的。

将请求与<代码>相读也没有任何问题。 页: 1 这个问题将使人们了解扼杀的内容,但我们确实可以谈论这一点,因为你没有提供任何相关信息。

HttpRequestStream won t give you the length, but you can get it from the HttpListenerRequest.ContentLength64 property. Like Jon said, make sure you observe the return value from the Read method. In my case, we get buffered reads and cannot read our entire 226KB payload in one go.

提 出

    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;
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签