Displaying Output as an HTML Page

 
Chapter 20 - Accessing the Internet
bySimon Robinsonet al.
Wrox Press 2002
  

Our examples show how the .NET base classes make it very easy to download and process data from the Internet. However, so far we have only displayed files as plain text. Quite often you will want to view an HTML file in an Internet Explorer style interface where the rendered HTML allows you to see what the web document actually looks like. Unfortunately, the .NET base classes don't include any intrinsic support for a control with an Internet Explorer-style interface. You will need to either programmatically call up Internet Explorer, or host the web browser as an ActiveX control.

You can programmatically start an Internet Explorer process and navigate to a web page using the Process class in the System.Diagnostics namespace.

   Process myProcess = new Process();     myProcess.StartInfo.FileName = "iexplore.exe";     myProcess.StartInfo.Arguments = "http://www.wrox.com";     myProcess.Start();   

However, the above code launches Internet Explorer as a separate window. Your application has no connection to the new window and therefore cannot control the browser.

On the other hand, using the browser as an ActiveX control allows you to display and control the browser as an integrated part of your application. The web browser control is quite sophisticated, featuring a large number of methods , properties, and events.

The easiest way to incorporate this control, using Visual Studio .NET, is to add the control to the toolbox. To do this, right-click on the toolbox in Visual Studio .NET and select Customize Toolbox from the context menu to bring up the following dialog. You should select the COM Components tab, and check Microsoft Web Browser .

click to expand

The Web Browser control now appears in the toolbox. You can click and drop the control onto your forms in the same manner as would you drop native .NET controls from the toolbox. Visual Studio .NET will automatically generate all the COM interoperability code required to host the web browser control in your application's form. We will demonstrate this technique with another example, DisplayWebPage , which will display a web page retrieved from the Internet in a typical Windows form.

We create DisplayWebPage as a standard C# Windows application, and drop the Web Browser ActiveX control onto the form. By default, Visual Studio .NET names the control axWebBrowser1 . We then add the following code to the Form1 constructor:

 public Form1() {   // Required for Windows Form Designer support   InitializeComponent();    int zero = 0;   object oZero = zero;     string emptyString = "";     object oEmptyString = emptyString;     axWebBrowser1.Navigate("http://www.wrox.com",     ref oZero,     ref oEmptyString,     ref oEmptyString,     ref oEmptyString);   } 

In this code we use the Navigate() method of the WebBrowser control, which actually sends an HTTP request and displays the output from a given URI. The first parameter to this method is a string containing the URI to navigate to. The second parameter accepts a number of flags to modify the navigation behavior, for example, if the browser adds the new URI to the history list or not. The third parameter contains the name of the target frame (if any) used to display the resource. The fourth parameter contains POST data to send with the request, and the final parameter allows you to pass additional HTTP header information. For our purposes, we can pass the default values for zero and the empty string into the last four parameters. These parameters are defined as optional parameters, but C# does not support optional parameters so we supply them explicitly. We also explicitly declare object references for these variables to pass them by reference.

Calling Navigate() with the parameters shown above is the same as typing the URL into the Internet Explorer address bar. This code is the only code we need to add to the DisplayWebPage project. If we run the example we get the results shown below (we have also used Visual Studio .NET to change the title text of the main form).

click to expand

The Web Request and Web Response Hierarchy

In this section we will take a closer look at the architecture underlying the WebRequest and WebResponse classes.

The inheritance hierarchy of the classes involved is shown in the following diagram.

click to expand

The hierarchy contains more than just the two classes we have used in our code. We also should point out that the WebRequest and WebResponse classes are both abstract, and cannot be instantiated . These base classes provide general functionality for dealing with web requests and responses independent of the protocol used for a given operation. Requests are made using a particular protocol (HTTP, FTP, SMTP, etc.) and a derived class written for the given protocol will handle the request. Microsoft refers to this scheme as "pluggable protocols". Remember in the code we examined earlier, our variables were defined as references to the base classes; however, WebRequest.Create() actually gave us an HttpWebRequest object, and the GetResponse() method actually returned an HttpWebResponse object. This factory-based mechanism provided by Microsoft hides many of the details from the client code, allowing support for a wide variety of protocols from the same code base.

The fact that we needed an object specifically capable of dealing with the HTTP protocol is clear from the URI that we supplied to WebRequest.Create() . WebRequest.Create() examines the protocol specifier in the URI to instantiate and return an object of the appropriate class. This keeps your code free from having to know anything about the derived classes or specific protocol used. When you need to access specific features of a protocol, you might need the properties and methods of the derived class, in which case you can cast your WebRequest or WebResponse reference to the derived class.

With this architecture we should be able to send requests using any of the common protocols. However, Microsoft currently only provides derived classes to cover the HTTP, HTTPS, and FILE protocols. If you want to utilize other protocols, for example, FTP or SMTP, then you will need to either fall back on the Windows API, write your own classes, or wait for an independent software vendor to write some the suitable .NET classes.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net