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 793713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:11:48+00:00 2026-05-14T22:11:48+00:00

I’m trying to read the response stream from an HttpWebResponse object. I know the

  • 0

I’m trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

Specified argument was out of the range of valid values.
Parameter name: size

While debugging, I noticed that at the time of the error, the values were as such:

length = 15032 //the length of the stream as defined by _response.ContentLength

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

bytesRead = 7680 //the number of bytes that have been read (offset)

body.length = 15032 //the size of the byte[] the stream is being copied to

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

Code:

int length = (int)_response.ContentLength;

byte[] body = null;

if (length > 0)
{
    int bytesToRead = length;
    int bytesRead = 0;

    try
    {
        body = new byte[length];

        using (Stream stream = _response.GetResponseStream())
        {
            while (bytesToRead > 0)
            {                                                        
                // Read may return anything from 0 to length.
                int n = stream.Read(body, bytesRead, length);

                // The end of the file is reached.
                if (n == 0)
                    break;

                bytesRead += n;
                bytesToRead -= n;
            }
            stream.Close();
        }
    }
    catch (Exception exception)
    {
        throw;
    }   
}
else
{
    body = new byte[0];
}

_responseBody = body;
  • 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-14T22:11:48+00:00Added an answer on May 14, 2026 at 10:11 pm

    You want this line:

    int n = stream.Read(body, bytesRead, length);
    

    to be this:

    int n = stream.Read(body, bytesRead, bytesToRead);
    

    You are saying the maximum number of bytes to read is the stream’s length, but it isn’t since it is actually only the remaining information in the stream after the offset has been applied.

    You also shouldn’t need this part:

    if (n == 0)
       break;
    

    The while should end the reading correctly, and it is possible that you won’t read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 389k
  • Answers 389k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The property decorator only works on new-style classes. In Python… May 15, 2026 at 12:38 am
  • Editorial Team
    Editorial Team added an answer You can either use the SQLite database in Android or… May 15, 2026 at 12:38 am
  • Editorial Team
    Editorial Team added an answer /etc/resolv.conf IS the authoritative source of DNS servers, so you… May 15, 2026 at 12:38 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.