O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

WebClient

WebClient provides a higher-level interface to network resources than WebRequest. DownloadData() and DownloadFile() retrieve resources from a URI into a byte array or a file. UploadData() and UploadFile() sends the contents of a byte array or a file to a resource identified by a URI. Use WebClient to rewrite Snarf.cs as:

// Snarf2.cs
// Run Snarf.exe <http-uri> to retrieve a web page
using System;
using System.Net;
using System.Text;
class Snarf {
  static void Main(string[] args) {
    WebClient wc = new WebClient();
    byte[] buffer = wc.DownloadData(args[0]);
    string doc = Encoding.ASCII.GetString(buffer);
    Console.WriteLine(doc);
  }
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required