472,986 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 14159
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DotNetJunkies User | last post by:
Hello, I am trying to download zip files over HTTPS with the HttpWebResponse object in C#. I've stepped thought the code and what seems to be happening is the Stream.Read method will only return...
2
by: David Billson | last post by:
Hi all, Fairly straight forward problem. I have a Post using HttpWebRequest that posts data to a server. That server then posts data back to me. Posting the data's not a problem, however I'm...
1
by: Vasu | last post by:
Hi, I have a requirement to download a file from the web site using a client tool. Iam writing a C# program to download using WebRequest, HttpRequest, WebResponse and so on. The problem...
15
by: Snedker | last post by:
I'm using (HttpWebRequest and HttpWebResponse to check for updates. But how do I determine the size of the file before download? What I have in mind is a status text like "You have downloaded...
3
by: mpar612 | last post by:
I am trying to create an area where a user can come to a page that queries a database and displays links to files. Here is the code for the link: <td><a...
4
by: Rakhi | last post by:
hello i want to alter the download settings of mozilla firefox browser using javascript of my application !! wat is happenin in my application is, on calling a method , it make a file ready...
2
by: =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post by:
Hi, I know there isn't a specific event property for the download speed, but can anyone tell me how to find it? I'm not sure how to write the code. Thanks, Jason
3
colinod
by: colinod | last post by:
I am trying to get my site to download mp3 files without having to right click - save as on a link. I have found this code, which works as long as the file names are short, if they get a bit long it...
4
by: tvnaidu | last post by:
I created an executable using static library instead shared lib, I am running multipe instances of this executable, does it takes more memory with static library compare to shared library?. Does...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.