471,350 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How to download an executable using HttpWebResponse

Greetings:

I need to download an executable and some other binary files (a C++
DLL) from a virtual directory in my WinForms application. No-Touch
Deployment manages my .NET code fine but auxilliary files such as these
must be downloaded "manually" in my code. My text-based files download
fine but I've ran into a problem downloading an .EXE. When it
downloads the file size is the same but it has been altered somehow
because a) the icon doesn't display in Explorer (a generic icon is
used) and b) the program creates a fault when I double-click it. I've
used a diff analyzer to compare the two files and there are some places
in the downloaded file that are empty where there should be data. Here
is the code I am using:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = @"application/octet-stream";
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;

try
{
response = (HttpWebResponse)request.GetResponse();
byte[] data = new byte[response.ContentLength];
response.GetResponseStream().Read(data, 0, data.Length);
return new MemoryStream(data);
}
finally
{
if(response != null)
{
response.Close();
}
}

Any assistance would be greatly appreciated!

Nov 29 '05 #1
2 14046
Steve,

Are you sure that you are actually getting all of the contents of the
file when you make the call to read? Check out the section of Jon Skeet's
FAQ titled "Reading binary data in C#", located at:

http://www.yoda.arachsys.com/csharp/readbinary.html

Given that, your code should look like this:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = @"application/octet-stream";
request.Credentials = CredentialCache.DefaultCredentials;

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
// Get the stream.
using (Stream stream = response.GetResponseStream())
{
// Use the ReadFully method from the link above:
byte[] data = ReadFully(stream, response.ContentLength);

// Return the memory stream.
return new MemoryStream(data);
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
response.GetResponseStream().Read(data, 0, data.Length);
return new MemoryStream(data);
}
finally
{
if(response != null)
{
response.Close();
}
}

<ST********@KY.GOV> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... Greetings:

I need to download an executable and some other binary files (a C++
DLL) from a virtual directory in my WinForms application. No-Touch
Deployment manages my .NET code fine but auxilliary files such as these
must be downloaded "manually" in my code. My text-based files download
fine but I've ran into a problem downloading an .EXE. When it
downloads the file size is the same but it has been altered somehow
because a) the icon doesn't display in Explorer (a generic icon is
used) and b) the program creates a fault when I double-click it. I've
used a diff analyzer to compare the two files and there are some places
in the downloaded file that are empty where there should be data. Here
is the code I am using:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = @"application/octet-stream";
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;

try
{
response = (HttpWebResponse)request.GetResponse();
byte[] data = new byte[response.ContentLength];
response.GetResponseStream().Read(data, 0, data.Length);
return new MemoryStream(data);
}
finally
{
if(response != null)
{
response.Close();
}
}

Any assistance would be greatly appreciated!

Nov 29 '05 #2
ST********@KY.GOV wrote:
Greetings:

I need to download an executable and some other binary files (a C++
DLL) from a virtual directory in my WinForms application. No-Touch
Deployment manages my .NET code fine but auxilliary files such as
these must be downloaded "manually" in my code. My text-based files
download fine but I've ran into a problem downloading an .EXE. When
it downloads the file size is the same but it has been altered somehow
because a) the icon doesn't display in Explorer (a generic icon is
used) and b) the program creates a fault when I double-click it. I've
used a diff analyzer to compare the two files and there are some
places in the downloaded file that are empty where there should be
data. Here is the code I am using:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.ContentType = @"application/octet-stream";
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;

try
{
response = (HttpWebResponse)request.GetResponse();
byte[] data = new byte[response.ContentLength];
response.GetResponseStream().Read(data, 0, data.Length);


That's wrong. You have to read from the stream until there's no more to
read.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by DotNetJunkies User | last post: by
2 posts views Thread by David Billson | last post: by
1 post views Thread by Vasu | last post: by
3 posts views Thread by mpar612 | last post: by
2 posts views Thread by =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.