We help IT Professionals succeed at work.

How do I download a file from a website programmatically?

Todd710
Todd710 asked
on
576 Views
Last Modified: 2012-05-08
I have started a project that downloads a file from a website.  I have tried several approaches.  The first one was the "WebClient wc = new WebClient();"  works good for text files but not binary.  I keep thinking this is a setting on the website but I have tried an apache webserver as well.  This approach only downloads a small piece of the file and does not error out.

The next approach is several snippets I got online same result.

Thanks for the help,
Todd
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace PatchDownload
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Stream stream = GetURLStream("http://www.website.com/filetodownload.msp");
            writeByteArrayToFile(ReadFully(stream, 0), @"C:\temp\download.msp");
        }
 
        public bool writeByteArrayToFile(byte[] buff, string fileName)
        {
            bool response = false;
 
            try
            {
                FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(buff);
                bw.Close();
                response = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
 
            return response;
        }
 
        private byte[] ReadFully(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }
 
            byte[] buffer = new byte[initialLength];
            int read = 0;
 
            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;
 
                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();
 
                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }
 
                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }
 
        private Stream GetURLStream(string strURL)
        {
            System.Net.WebRequest objRequest;
            System.Net.WebResponse objResponse = null;
            Stream objStreamReceive;
 
            try
            {
                objRequest = System.Net.WebRequest.Create(strURL);
                objRequest.Timeout = 5000;
                ((HttpWebRequest)objRequest).UserAgent = "Test.Net Client";
                objResponse = objRequest.GetResponse();
                objStreamReceive = objResponse.GetResponseStream();
 
                return objStreamReceive;
            }
            catch (Exception excep)
            {
                Debug.WriteLine(excep.Message);
                objResponse.Close();
 
                return null;
            }
        }
    }
}

Open in new window

Comment
Watch Question

Nate FeinbergFinancial Center Operations Manager

Commented:
When you tried WebClient, did you try its instance method DownloadFile(uri, fileName)? It worked alright for me.
- Nate
System Administrator
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I have used your specific code example with the same result.  I have tried to dowload .zip, .cab, and .msp from 3 different webservers(Appache, IIS6, IIS7) with the same result only a few bytes or none to the download location. I can downoad each of these files from the website using the URL in IE.  I even changed the download location thinking that might something wrong with the folder.  I am fairly mystified
Tomasz CzyzSystem Administrator

Commented:
Try to use debuging. Make sure that the hole file is download before closing it. My code has client_DownloadFileCompleted that means that the download is finished when it is called.

If you close your program or thread before totally finishing the download it will be only some bytes.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.