473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Content Type = @"applicatio n/octet-stream";
request.Credent ials = CredentialCache .DefaultCredent ials;
HttpWebResponse response = null;

try
{
response = (HttpWebRespons e)request.GetRe sponse();
byte[] data = new byte[response.Conten tLength];
response.GetRes ponseStream().R ead(data, 0, data.Length);
return new MemoryStream(da ta);
}
finally
{
if(response != null)
{
response.Close( );
}
}

Any assistance would be greatly appreciated!

Nov 29 '05 #1
2 14208
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.Content Type = @"applicatio n/octet-stream";
request.Credent ials = CredentialCache .DefaultCredent ials;

using (HttpWebRespons e response = (HttpWebRespons e) request.GetResp onse())
{
// Get the stream.
using (Stream stream = response.GetRes ponseStream())
{
// Use the ReadFully method from the link above:
byte[] data = ReadFully(strea m, response.Conten tLength);

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
response.GetRes ponseStream().R ead(data, 0, data.Length);
return new MemoryStream(da ta);
}
finally
{
if(response != null)
{
response.Close( );
}
}

<ST********@KY. GOV> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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.Content Type = @"applicatio n/octet-stream";
request.Credent ials = CredentialCache .DefaultCredent ials;
HttpWebResponse response = null;

try
{
response = (HttpWebRespons e)request.GetRe sponse();
byte[] data = new byte[response.Conten tLength];
response.GetRes ponseStream().R ead(data, 0, data.Length);
return new MemoryStream(da ta);
}
finally
{
if(response != null)
{
response.Close( );
}
}

Any assistance would be greatly appreciated!

Nov 29 '05 #2
ST********@KY.G OV 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.Content Type = @"applicatio n/octet-stream";
request.Credent ials = CredentialCache .DefaultCredent ials;
HttpWebResponse response = null;

try
{
response = (HttpWebRespons e)request.GetRe sponse();
byte[] data = new byte[response.Conten tLength];
response.GetRes ponseStream().R ead(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.d e
Nov 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2101
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 1040 Bytes from the stream regardless of the size of the file. Is anyone else having this problem?? Cole --- Posted using Wimdows.net NntpNews Component -
2
24707
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 wondering if there is an easy way to read back the variables that are returned. If not, I'm left creating a parse routine which is not the end of the world but I just want to be as programatically correct as I can be. Code:
1
5872
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 Iam having is to navigate thru multiple pages. I have to login
15
6397
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 xxxx of yyyy bytes" I'm trying to find the y's within this: ---
3
1622
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 href="download.php?path=../ftp/$path&name=$name\" target=\"_blank\">$name</a></font></b></td> header information to download the file. This is the only code that I have in download.php:
4
1924
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 to be downloaded . and the browser gives the download window ,like.., save file or open file etc.. as we generally see. i want the browser to ask the location to the user .. as to where he
2
5822
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
6439
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 just opens a blank page, can anyone help <%@Language="VBScript"%> <%Option Explicit%> <%Response.Buffer = True%> <% On Error Resume Next Dim strPath strPath = CStr(Request.QueryString("file"))
4
4439
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 shared library had any advantage than static library in multiple instances?. thanks in advance
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.