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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:05:32+00:00 2026-06-04T10:05:32+00:00

Why am I missing bytes when reading from a WebClient stream as follows? const

  • 0

Why am I missing bytes when reading from a WebClient stream as follows?

const int chuckDim = 80;

System.Net.WebClient client = new System.Net.WebClient();
Stream stream = client.OpenRead("http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg");
//Stream stream = client.OpenRead("file:///C:/Users/Tanganello/Downloads/needed-backup-lol.jpg");

//searching file length
WebHeaderCollection whc = client.ResponseHeaders;
int totalLength = (Int32.Parse(whc["Content-Length"]));
byte[] buffer = new byte[totalLength];

//reading and writing
FileStream filestream = new FileStream("C:\\Users\\Tanganello\\Downloads\\clone1.jpg", FileMode.Create, FileAccess.ReadWrite);
int accumulator = 0;
while (accumulator + chuckDim < totalLength) {
    stream.Read(buffer, accumulator, chuckDim);
    filestream.Write(buffer, accumulator, chuckDim);

    accumulator += chuckDim;
}
stream.Read(buffer, accumulator, totalLength - accumulator);
filestream.Write(buffer, accumulator, totalLength - accumulator);

stream.Close();
filestream.Flush();
filestream.Close();

this is what I get with the first stream:
http://img839.imageshack.us/img839/830/clone1h.jpg

  • 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-06-04T10:05:33+00:00Added an answer on June 4, 2026 at 10:05 am

    The problem is that you are ignoring the return value of the Stream.Read Method:

    count

    The maximum number of bytes to be read from the current stream.

    Return Value

    The total number of bytes read into the buffer. This can be less than the number of bytes requested


    You can avoid the whole business of reading and writing streams by simply using the WebClient.DownloadFile Method:

    using (var client = new WebClient())
    {
        client.DownloadFile(
            "http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg",
            "C:\\Users\\Tanganello\\Downloads\\clone1.jpg");
    }
    

    Alternatively, if you really want to use streams, you can simply use the Stream.CopyTo Method:

    using (var client = new WebClient())
    using (var stream = client.OpenRead("http://..."))
    using (var file = File.OpenWrite("C:\\..."))
    {
        stream.CopyTo(file);
    }
    

    If you insist on really copying the bytes yourself, the correct way to do this would be as follows:

    using (var client = new WebClient())
    using (var stream = client.OpenRead("http://..."))
    using (var file = File.OpenWrite("C:\\..."))
    {
        var buffer = new byte[512];
        int bytesReceived;
        while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            file.Write(buffer, 0, bytesReceived);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading from a pipe: unsigned int sample_in = 0; //4 bytes - 32bits, right?
I'm having an issue reading from a java input stream. I have a buffer
I'm probably missing something obvious here but here's what I'm trying to do. From
I have a client TCP socket that writes a few bytes every five seconds,
I'm reading in an NES ROM file, where the first four bytes are \x4e\x45\x53\x1a,
I have a problem with the boost::asio::serial_port class reading from a GPS device (USB-Serial).
First, let me explain the current situation: I'm reading records from a database and
I am reading the pdf in byte array from my WCF web service and
Comining from my question 8078 bytes in 8060 B datapage (SQL Server)? where it
Int is 4 bytes with a range of +- 2^31 Float is 4 bytes

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.