Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:31:53+00:00 2026-05-28T03:31:53+00:00

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

  • 0

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

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

I want to read the stream (which contain non-Character content, because i’ve sent the packet)

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…

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

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

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

I’ve solved it by the following

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:31:54+00:00Added an answer on May 28, 2026 at 3:31 am

    Your bug lies in the following line:

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

    The byte type is unsigned; when Stream.ReadByte returns -1 at the end of the stream, you’re indiscriminately casting it to byte, which converts it to 255 and, therefore, satisfies the b >= 0 condition. It is helpful to note that the return type is int, not byte, for this very reason.

    A quick-and-dirty fix for your code:

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

    The following solution is more efficient, since it avoids the byte-by-byte reads incurred by the ReadByte calls, and uses a dynamically-expanding byte array for Read calls instead (similar to the way that List<T> is internally implemented):

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP code which simply grabs a URL behind HTTP-Basic authentication
I have setup a basic app which pulls down a JSON stream and displays
Currently, i have basic C++ and PHP skills. But, i want to switch to
I have a basic model in which i have specified some of the fields
I have a basic ActiveRecord model in which i have two fields that i
I have a basic socket server that looks like this: IPEndPoint localEndPoint = new
I have a WCF service which has one method returning a stream. [ServiceContract] public
i have rather very basic question. I have one GUI class and another which
I have recently used Visual Basic .Net to write a particle system which emits
After Twitter discontinuing the Basic Auth, my program which updates my own Twitter stream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.