Solved

HttpWebRequest doesnt work but WebClient does ?? Why??

Posted on 2006-10-27
2
236 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 52
insert value of checklistbox checked 4 32
Amazon S3 .Net error 5 17
need to convert function to c# 5 29
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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