15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Displaying output as an HTMl Page ❘ 643<br />

}<br />

wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq);<br />

protected static void OnResponse(IAsyncResult ar)<br />

{<br />

WebRequest wrq = (WebRequest)ar.AsyncState;<br />

WebResponse wrs = wrq.EndGetResponse(ar);<br />

// read the response...<br />

}<br />

Notice that you can retrieve the original WebRequest object by passing the object as the second parameter<br />

to BeginGetResponse(). The second parameter is an object reference known as the state parameter.<br />

During the callback method, you can retrieve the same state object using the AsyncState property of<br />

IAsyncResult.<br />

disPlaying ouTPuT as an hTml Page<br />

The examples so far in this chapter show how the .<strong>NET</strong> base classes make it very easy to download<br />

<strong>and</strong> process data from the Internet. However, so far you have displayed files only as plain text. Quite<br />

often, you will want to view an HTML file in an Internet Explorer-style interface in which the rendered<br />

HTML allows you to see what the web document actually looks like. Unfortunately, there is no<br />

.<strong>NET</strong> version of Microsoft’s Internet Explorer, but that does not mean that you cannot easily<br />

accomplish this task.<br />

Before the release of the .<strong>NET</strong> Framework 2.0, you could make reference to a Component Object Model<br />

(COM) object that was an encapsulation of Internet Explorer <strong>and</strong> use the .<strong>NET</strong>-interop capabilities to have<br />

aspects of your application work as a browser. Now, since the release of the .<strong>NET</strong> Framework 2.0, you can<br />

use the built-in WebBrowser control available for your Windows Forms applications.<br />

The WebBrowser control encapsulates the COM object even further for you <strong>and</strong> makes the tasks that were<br />

once more complicated, even easier. In addition to the WebBrowser control, another option is to use the<br />

programmatic ability to call up Internet Explorer instances from your code.<br />

When not using the new WebBrowser control, you can programmatically start an Internet Explorer process<br />

<strong>and</strong> navigate to a web page using the Process class in the System.Diagnostics namespace:<br />

Process myProcess = new Process();<br />

myProcess.StartInfo.FileName = "iexplore.exe";<br />

myProcess.StartInfo.Arguments = "http://www.wrox.com";<br />

myProcess.Start();<br />

However, the preceding code launches Internet Explorer as a separate window. Your application has no<br />

connection to the new window <strong>and</strong> therefore cannot control the browser.<br />

Using the new WebBrowser control, however, allows you to display <strong>and</strong> control the browser as an integrated<br />

part of your application. The new WebBrowser control is quite sophisticated, featuring a large number of<br />

methods, properties, <strong>and</strong> events.<br />

allowing simple Web browsing from your applications<br />

For the sake of simplicity, start by creating a Windows Forms application that simply has a TextBox control<br />

<strong>and</strong> a WebBrowser control. You will build the application so that the end user will simply enter a URL into<br />

the text box <strong>and</strong> press Enter, <strong>and</strong> the WebBrowser control will do all the work of fetching the web page <strong>and</strong><br />

displaying the resulting document.<br />

In Visual Studio 2010 Designer, your application should look like Figure 24-3.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!