
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
804
|
Chapter 14: Web
Solution
You can use the methods for web communication that were set up in Recipes 14.5
and 14.6 to make the HTTP request and verify the response; then, you can get at the
HTML via the
ResponseStream property of the HttpWebResponse object:
public static string GetHtmlFromUrl(string url)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentNullException("url","Parameter is null or empty");
string html = "";
HttpWebRequest request = GenerateHttpWebRequest(url);
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse( ))
{
if (VerifyResponse(response) == ResponseCategories.Success)
{
// Get the response stream.
Stream responseStream = response.GetResponseStream( );
// Use a stream reader that understands UTF8.
using(StreamReader reader =
new StreamReader(responseStream, Encoding.UTF8))
{
html = reader.ReadToEnd( );
}
}
}
return html;
}
Discussion
The GetHtmlFromUrl method gets a web page using the GenerateHttpWebRequest and
GetResponse methods, verifies the response using the VerifyResponse method, and
then, once it has a valid response, starts looking for the HTML that was returned.
The
GetResponseStream method on the HttpWebResponse provides access to the body
of the message that was returned in a
System.IO.Stream object. In order to read the
data, instantiate ...