O'Reilly logo

Programming C# 4.0 by Jesse Liberty, Matthew Adams, Ian Griffiths

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

HTTP

The .NET Framework class library provides various classes for working directly with HTTP. Some of these are for client scenarios, and are useful when you need to fetch resources from a web server such as bitmaps, or if you need to use an HTTP-based service that WCF cannot easily work with. You can also provide server-side HTTP support. You would normally do that by writing an ASP.NET web application, which we’ll look at in a later chapter. But there is a class that enables other program types to receive incoming HTTP requests, called HttpListener. (We won’t be covering that, and we mention it mainly for completeness—it’s more normal to use ASP.NET, to which we have devoted a whole chapter.)

WebClient

The most common starting point for client-side HTTP code is the WebClient class in the System.Net namespace. It offers a few ways of working with HTTP, starting from very simple but inflexible methods, through to relatively complex mechanisms that give you complete control over detailed aspects of HTTP. We’ll start with the simplest ones.

Note

Although the examples in this section are HTTP-based, WebClient supports other protocols, including https:, ftp:, and file: URLs. It is extensible, so in principle you can adapt it to support any protocol that has a URL scheme.

Downloading resources

Example 13-11 illustrates one of the simplest ways of using the WebClient class. We construct an instance, and then use its DownloadString method to fetch data at a particular URL. (You can specify ...

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