title: Read from basic stream (httpRequestStream)
body: <p>I have a basic stream which is the stream of HTTP request 
  and </p>
  
  <pre><code>var s=new HttpListener().GetContext().Request.InputStream;
  </code></pre>
  
  <p>I want to read the stream (which contain non-Character content, because i've sent the packet)</p>
  
  <p>When we wrap this stream by StreamReader then we use the ReadToEnd() function of StreamReader it can read the whole stream and return a string...</p>
  
  <pre><code>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
  </code></pre>
  
  <p>but since it has nonCharacter content we cant use StremReader (i tried all encoding mechanisms..using string is just wrong).And i Cant use the function </p>
  
  <pre><code>context.Request.InputStream.Read(buffer,position,Len) 
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>List&lt;byte&gt; bb = new List&lt;byte&gt;();
  var ss = context.Request.InputStream;
  byte b = (byte)ss.ReadByte();
  while (b &gt;= 0)
  {
      bb.Add(b);
      b = (byte)ss.ReadByte();
  }
  </code></pre>
  
  <p>I've solved it by the following </p>
  
  <pre><code>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 &gt; 0)
  {
      fs.Write(file, 0, finishedBytes);
      finishedBytes = ss.Read(file, 0, file.Length);
  }
  fs.Close();
  </code></pre>
  
  <p>thanks Jon , Douglas</p>