Web Streams
Instead of 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 resource identified by a 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 access to the object pointed to by the URI. What is returned 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, 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 a URI, an object of type HttpWebRequest ...
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.
Read now
Unlock full access