Solved

HttpWebRequest doesnt work but WebClient does ?? Why??

Posted on 2006-10-27
2
232 Views
Last Modified: 2010-04-16
I have a file on my server that produces a certain sha1 hashcode.
but when i download that same file using httpwebrequest the downloaded file has a different hashcode?

     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(bu);
     HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

but if I use the WEBCLIENT class instead i get the correct hashcode !!

so am i missing some specification info for the httpwebrequest object maybe?
i am using vs2005
i need to be able to use the httpwebrequest
0
Comment
Question by:joshuadavidlee
2 Comments
 
LVL 2

Accepted Solution

by:
andrewjmears earned 500 total points
Comment Utility
Use free Fiddler tool (HTTP Debugging Proxy) from Microsoft (http://www.fiddlertool.com/fiddler/) to see what might be different in the HTTP request/response. It might be an encoding issue and you may need to specify the encoding when you read the stream.
0
 

Author Comment

by:joshuadavidlee
Comment Utility
this is what solved the problem for me:
i sent the response buffer into this method and it worked:

  # region ReadFully


        /// <param name="stream">The stream to read data from</param>
        /// <param name="initialLength">The initial buffer length</param>
        public byte[] ReadFully(Stream stream, int initialLength)
        {
            /* If we've been passed an unhelpful initial length, just
             use 32K. */
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                /* If we've reached the end of our buffer, check to see if there's
                 any more information */
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    /* End of stream? If so, we're done */
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    /* Nope. Resize the buffer, put in the byte we've just
                     read, and continue */
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            /* Buffer is now too big. Shrink it. */
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }
        #endregion
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now