How to download an executable using HttpWebResponse

  • Thread starter Thread starter STEVE.KING
  • Start date Start date
S

STEVE.KING

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!
 
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]
- (e-mail address removed)
response.GetResponseStream().Read(data, 0, data.Length);
return new MemoryStream(data);
}
finally
{
if(response != null)
{
response.Close();
}
}


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!
 
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top