Skip to Main Content
Programming C#
book

Programming C#

by Jesse Liberty
July 2001
Intermediate to advanced content levelIntermediate to advanced
688 pages
16h 14m
English
O'Reilly Media, Inc.
Content preview from Programming C#

Web Streams

Rather than reading from a stream provided by a custom server, you can just as easily read from any web page on the Internet.

A WebRequest is an object that requests a Uniform Resource Identifier (URI) such as the URL for a web page. You can use a WebRequest object to create a WebResponse object that will encapsulate the object pointed to by the URI. That is, you can call GetResponse( ) on your WebRequest object to get the actual object (e.g., a web page) pointed to by the URI. What you get back is encapsulated in a WebResponse object. You can then ask that WebResponse object for a Stream object by calling GetResponseStream(). GetResponseStream( ) returns a stream that encapsulates the contents of the web object (e.g., a stream with the web page).

The next example retrieves the contents of a web page as a stream. To get a web page, you’ll want to use HttpWebRequest. HttpWebRequest derives from WebRequest and provides additional support for interacting with the HTTP protocol.

To create the HttpWebRequest you cast the WebRequest returned from the static Create( ) method of the WebRequestFactory:

HttpWebRequest webRequest = 
    (HttpWebRequest) WebRequest.Create
    ("http://www.libertyassociates.com/book_edit.htm");

Create( ) is a static method of WebRequest. When you pass in a URI, an instance of HTTPWebRequest is created.

Tip

The method is overloaded on the type of the parameter. It returns different derived types depending on what is passed in. For example, if you pass in ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming C#, Second Edition

Programming C#, Second Edition

Jesse Liberty
Programming C# 12

Programming C# 12

Ian Griffiths
Programming C# 8.0

Programming C# 8.0

Ian Griffiths

Publisher Resources

ISBN: 0596001177Supplemental ContentCatalog PageErrata