Click here to Skip to main content
Click here to Skip to main content
Go to top

C# Web File Download with BITSadmin

, 27 Mar 2013
Rate this:
Please Sign up or sign in to vote.
When WebClient, HttpRequest, and all else fail, BITSadmin solves your download woes

Introduction

I was making an app that required downloading files from a website.  In the past, I’ve used WebClient (excellent for most scenarios), HttpWebRequest (clunky), and even the Forms WebBrowser (very messy) to accomplish the deed.  For this particular app however, all of these methods failed due to the dreaded “server protocol violation” error.  Argh!!!  With some digging, I found a console executable that was right for the job, so I made a simple method to use it in my app.  Success!  

Using the code 

A brief description of how to use the article or code. The
class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted"
like this:

string local = @”C:\Users\metastruct\Desktop\my_image.jpg”;

string remote = @”http://www.test.com/some_image.jpg”;

download(remote, local);

That easy to use!

The Code

private void download(string remote_file_url, string local_file)
{
    try
    {
        /* Create the Process */
        System.Diagnostics.Process bitsadmin_process = new System.Diagnostics.Process();
        /* Assign the BITSadmin Executable and Arguements to the Process */
        bitsadmin_process.StartInfo = new System.Diagnostics.ProcessStartInfo(“bitsadmin”, “/transfer mydownloadjob  /download /priority normal “ + remote_file_url + ” “ + local_file);
        /* Start the Process */
        bitsadmin_process.Start();
        /* Wait While the File Downloads */
        bitsadmin_process.WaitForExit();
        /* Dispose the Process */
        bitsadmin_process = null;
    }
    catch { }

    return;
} 

Such a simple solution to such an annoying problem.  Caution: downloading to a share drive gave me issues, so I downloaded locally and used File.Copy() to transfer.  Next step would be some improved error handling/redirection from the Process, but this works for now. Check out more at http://meta-struct.com/ 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

metastruct
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralToo bad ;) PinmemberMember 979571828-Mar-13 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 27 Mar 2013
Article Copyright 2013 by metastruct
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid