Integrate through Web Interfaces with C#

Environment: .NET Runtime on Windows

In today’s enterprise environments, there are many applications in a company, big or small. Quite often, some of these applications need to work together. That’s where integration comes into play. People often talk about Web Services, CORBA, COM, and so forth. These fancy things make up the middleware part. For some, these things might be needed; for others, they might be resolved through the idea proposed in this article: integrate through Web interfaces. The latter solution may be proved to be universally applied, low cost, time saving, and little responsibility for the applications to be integrated (this is the biggest headache problem within an enterprise).

In the past several years, most enterprise applications have been Web enabled, which means they have Web interfaces for users and almost surely for administrators. For instance, some old mainframe applications have done so through WebSphere from IBM. Newer applications may find it impossible to not have a Web interface in this World Wide Web era. More often, many of these applications do not provide the APIs or the APIs that are hard for others to use because of the platform, programming languages, and so on. If the project you work on asks for help from other projects, others may have no time or may not be willing to help or even have no way to help (the system is from a third party). This is where integrating through Web interfaces comes to the rescue. The author has been a technical lead for a project at AT&T Wireless Service, Inc. for the past two years, and found this is a way of fast, low cost, and little help from third parties.

For the concrete technical details, this article only talks about the essential part of using C# to do the integration work. The author also used Java technologies to do the same thing for some cases with less satisfying results, but he would like to talk about it in another place because this forum is not focused on Java technologies.

Many may wonder how to use the System.Web package in .NET. The author has tried, but found it is probably not feasible because it’s hard to get the tablized data and handle the scripting in the HTML pages (but the author hopes readers can explore to see whether the idea of this article can be realized with this package. If so, it would be a greater improvement of this article). What the author used are microsoft.mshtml.dll and interop.SHDocVw.dll. Their documentation can be found at MSDN Web site.

The following code for doing the integration creates an instance of IE and programmingly interacts with the instance: submit a query and retrieve data.

public static void OpenBrowser(string url)
  object o = null;
  InternetExplorer ie  = null;
    ie = new InternetExplorerClass();
    IWebBrowserApp wb = (IWebBrowserApp) ie;
    wb.Navigate(url, ref o, ref o, ref o, ref o);
    wb.Visible = true;
    // there are should be some better way to tell if it is ready.
    while (wb.Busy);
    HTMLDocument wd = (HTMLDocument)wb.Document;
    HTMLTable myTable;

    Object input = wd.all.item("para1",0);
    if (input == null )
      Console.WriteLine("no para1");
    HTMLInputElement e = (HTMLInputElement) input;
    e.value = " the test data submitted";
    HTMLInputElement e2 = (HTMLInputElement)wd.all.item("Submit",0);
    mshtml.HTMLFormElement he =
          (HTMLFormElement)wd.forms.item("mytest", 0);
    if (ie != null)

The following code shows how to retrieve the data in a table, which is used almost in any presentation. One may retrieve any other data in a similar way; just consult the documentation. Please note that there is a trick to cast the data object in a cell to the IHTMLElement type so that the data can be easily retrieved.

myTable = (HTMLTable)wd.getElementById("tid");
IHTMLElementCollection ic = wd.getElementsByTagName("table");

System.Collections.IEnumerator ice = ic.GetEnumerator();
HTMLTable theSameTable = (HTMLTable)ice.Current;
// two ways to get the same table.
Console.WriteLine("There are two ways to get a table:");
Console.WriteLine(" Are tables myTable and theSameTable the same,
                    myTable==theSameTable? " +
                   (myTable==theSameTable) + ".n");

//Print the table data to console.
Console.WriteLine("Here is the data from a table: it will be used
                   by your application.");
System.Collections.IEnumerator ee = myTable.rows.GetEnumerator();
  HTMLTableRow row = (HTMLTableRow) ee.Current;
  IHTMLElementCollection cells = row.cells;
  System.Collections.IEnumerator ee2 = cells.GetEnumerator();
  while (ee2.MoveNext())
    IHTMLTableCell aCell = (IHTMLTableCell) ee2.Current;
    IHTMLElement el = (IHTMLElement)aCell;
    Console.WriteLine( "Cell=" + row.rowIndex + "," +
                        aCell.cellIndex  + "..."+ el.innerText);

One additional nice thing is that all session states are handled by the IE instance. As long as the Internet browser is configured properly, one does not worry about proxy, HTTPS, and the like.

In summary, this article proposes a way to integratethe enterprise applications through Web interfaces. Because the Web interfaces are widely available for many existing applications, the method may well reduce the cost of building an integration system with less time and get rid of the dependency on other resources.


Download demo project – 1,697 Kb

More by Author

Must Read