Solved

HttpWebRequest doesnt work but WebClient does ?? Why??

Posted on 2006-10-27
2
235 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
ID: 17824407
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
ID: 17824821
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 28
C#  Radio button search for Date not DateTime 4 46
how to check to see if datatable has headers or not 2 29
PrintingFoundIt(Almost!(Damn!)) 1 27
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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