×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

(OP)
I have used this code other places with no problem. The site I'm trying to download from now is always coming back as corrupt. Is there something else I need to add to this code to successfully bring this file down to my machine? Here's my code.



    string strVars = "";  

            try
            {
                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", strPage, strVars));
                string[] h;
                if (headers.Length > 0)
                {
                    h = headers.Split(Convert.ToChar(","));
                    foreach (string s in h)
                    {
                        WebReq.Headers.Add(s);
                    }
                }


                WebReq.Method = "GET";
                                
                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
                        
                Stream Answer = WebResp.GetResponseStream();
                StreamReader _Answer = new StreamReader(Answer);
                StreamWriter sw = new StreamWriter(fileName,false);
                sw.WriteLine(_Answer.ReadToEnd());
                
sw.Close();
 

RE: getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

Quote:

I have used this code other places with no problem. The site I'm trying to download from now is always coming back as corrupt.
if the code hasn't changed, then the code isn't the problem.
have the server's changed?
are the servers configured differently?
has the file changed?
are you sure the file is not corrupt?
what headers are passed with the response?

Jason Meckley
Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

RE: getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

(OP)
Server on the other end is not one we have ever downloaded from before.  If I browse to the site and click on the dropdown and the button for download - the file is saved to my machine just fine.
The corrupt version is about a million bytes (almost double) bigger than the one I downloaded manually.  Looks like perhaps a problem with the character set??  I am not sending headers to them. I'm getting back

{Content-disposition: inline; filename="IpToCountry.csv.gz"
Connection: close
Content-Length: 1290205
Content-Type: application/octet-stream
Date: Thu, 01 Sep 2011 19:37:14 GMT
Server: Apache

}    

The content length shows the number of bytes I should be storing. Perhaps I just need to alter my StreamWriter to only write out that number of bytes or replace the ReadToEnd with Read and the number of bytes I want?


 

RE: getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

(OP)
I had to do two things to get this to download successfully.  I added encoding with the value of  "28591", which is the .NET reference for ISO 8859-1.

And i had to change the WriteLine on the StreamWriter to just Write to avoid add a LineFeed CarriageReturn at the end which apparently corrupts the file.
 

RE: getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

I think that's the reason you need to use the binarywriter class.
Streamwriter is used for strings (and hence the encoding problems)

http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx

/Daddy

-----------------------------------------------------
 What You See Is What You Get
Never underestimate tha powah of tha google!

RE: getting corrupt zip and gz files with HttpWebRequest &HttpWebResponse

(OP)
Thanks Jason and Daddy for your feedback!!!
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close