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:
download(remote, local);
That easy to use!
The Code
private void download(string remote_file_url, string local_file)
{
try
{
System.Diagnostics.Process bitsadmin_process = new System.Diagnostics.Process();
bitsadmin_process.StartInfo = new System.Diagnostics.ProcessStartInfo(“bitsadmin”, “/transfer mydownloadjob /download /priority normal “ + remote_file_url + ” “ + local_file);
bitsadmin_process.Start();
bitsadmin_process.WaitForExit();
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/