Wednesday, October 18, 2006

Using FTP or HTTP to download data


If you have the need to download data via FTP or HTTP then this little snippet is your solution.

Use the System.Net.WebClient to achieve this. The URI determines whether a file (file://) , FTP (FTP://) or HTTP (HTTP:// or HTTPS://) scheme is to be used.

For every method on this class there is an equivalent Async version (added in .net 2.0). This allows you download data asynchronously as a background task using its own thread from the thread pool.

The WebClient object can only handle one async download at a time. This is not suitable for downloading many files concurrently (you could use multiple WebClient objects). Cancel the current download using CancelAsync.

Methods:

OpenRead - returns a System.IO.Stream
OpenReadAsync - async downloading use the handler OpenReadCompleted

DownloadData - returns a ByteArray
DownloadDataAsync - use handler DownloadDataCompleted

DownloadFile - Saves to a local file
DownloadFileAsync - use handler DownloadFileCompleted

DownloadString - returns a String ( introduced in .net 2.0)
DownloadStringAsync - use handler DownloadStringCompleted

** you can also upload files using this class (OpenWrite, UploadData, UploadFile, UploadString) and the Async versions of each.

** if you have a proxy then you will need extra code to code around it. You will need to validate against the proxy before using the webclient. [code to follow in another topic]

Sample code:

using System.IO;
using System.Net;
using System.Text.RegularExpressions;

string remoteUri = "http://www.RipThatPage.com";
WebClient client = new WebClient();
string str = client.DownloadString(remoteUri);

No comments: