|
Simulate SOAP and Web Services广告 Simulate SOAP and Web Services
by Dino Esposito XML and SOAP are two hot topics, and with good reason. The XML
(Extensible Markup Language) format is widely accepted as a sort of new and more
powerful ASCII table. And the SOAP (Simple Object Access Protocol) distributed
computing technology boasts the cross-platform capabilities, flexibility, and
ease of use found in HTTP and XML. In fact, you could say SOAP technology was an
inevitable "next step" following the broad acceptance of HTTP and XML. Here's
why I say that. As Davide Marcato explained in his article in the April 2000 issue of VCDJ, SOAP can be considered an HTTP special edition for remote method invocation (see Resources). With today's technology, you can write a pair of proxy/stub modules to prepare, send, and receive SOAP payloads—and that's exactly what several companies, including Microsoft and DevelopMentor, are doing. Most of us, however, are waiting on the development of some type of object model or a simplified API—as we saw in the evolution of HTTP—to make SOAP useful on a broad scale. Meanwhile, you already might be facing the problem of setting up XML-based communications between remote sources. SOAP is a key element in the next generation of COM components, called Web Services because of their ability to fulfill XML-based requests coming through HTTP from a platform-independent client. If you plan to use SOAP—and you should—you might want to check out the VC++ SOAP toolkit available from the MSDN home page. But in addition to that toolkit, I suggest you consider a little known but free runtime component called XmlHttpRequest. It comes as a constituent part of the Microsoft XML parser (MSXML) and, as such, can be redistributed as a separate component with your applications. In this article, I show you how to use XmlHttpRequest to exchange any sort of information with remote URLs: no matter what platform or language is involved. Before you begin working with SOAP, you should take a closer look at this useful component. Manage HTTP Packets XmlHttpRequest is not as cross-platform-capable as a real Web Service client should be. In fact, the component works best when communicating with URLs that are ASP pages. XmlHttpRequest requires a COM-aware client, and it can't yet be used for server-to-server communications (more on this later). However, if you're writing a Win32 or Web application today and can afford a client-side COM object to manipulate the HTTP low-level stuff, XmlHttpRequest is your best tool for the job. Primarily, XmlHttpRequest lets you send HTTP packets to a specified URL, both synchronously and asynchronously. On top of this core functionality, you can build any object model you need, including a SOAP object model. Frankly speaking, I expect Microsoft to develop a future version of XmlHttpRequest or a similar object that allows you to call remote methods through a natural programming interface and take advantage of SOAP under the hood. (No, I don't consider MyObject.asp?method=Foo to be a natural programming interface!) Working with XmlHttpRequest involves two basic steps, familiar to anyone who knows a bit of WinInet. (Later in this article, I'll talk about how these steps represent a significant drawback for the XmlHttpRequest object.) The first step: Provide the component with the minimal information it needs to get in touch with the URL. To begin, open the remote connection by specifying the URL, the HTTP command (GET, POST, PUT, and so on) to be issued, and whether you want the connection to run synchronously or asynchronously (the default). To wrap up the first step, enter any user ID and password necessary for authentication purposes; if you don't provide such security information for a Web server that requires it, a logon dialog box asks you to enter a username and password before continuing (see Figure 1). When the HTTP command terminates, the application can get output results in a variety of formats. The output is available through different properties as raw text, an array of unsigned chars, an IStream pointer or an XML object model (XMLDOM). This list of available output formats reveals the XmlHttpRequest component's nature as an HTTP object model with special support for XML data. Typically, programmers use XmlHttpRequest to ask an aware Web page to return a configurable excerpt of its information. The caller can then use the information to refresh or populate its user interface. In other words, XmlHttpRequest allows you to consider any Web site or server-side application accessible over HTTP as a sort of external, cross-platform library. Although XmlHttpRequest has nothing to do with SOAP, you can use it to simulate SOAP functionality. You also can use it to put together a high-level object model to invoke methods on any remote object, including ASP pages; CGI applications written in Java, Perl, C, or even Visual Basic; ISAPI DLLs; or Java servlets. XmlHttpRequest in Action url = "http://expoware/test.asp" The response you get is everything the URL writes to its stdout console. If the URL is an ASP page, you get back every single byte, written through Response.Write. For example, suppose test.asp is the next simple server page: <% // cgitest.c : Entry point for the printf("200 OK\r\n"); url = "http://expoware/cgi-bin/cgitest.exe" The key advantage of XmlHttpRequest is its ability to handle XMLDOM. The component allows you to send and receive XML data over the Web through the document object model. And, it takes care of marshaling the XMLDOM back and forth through the Web server. You can send your IDispatch pointer to XMLDOM through the send() method: Set http = ' TODO: ' Send back XML content printf("Content-Type: text/xml\r\n"); I used this object recently to build a free viewer of extremely volatile information. I wrote a pretty lean C++ application that connects to an ASP page and downloads all the information it needs to populate the user interface (see Listing 1). The information is displayed in an edit box (see Figure 3). The ASP page was simply a new one I added to the Web site; I made it invisible to and unreachable from the other pages (see Listing 2). Data sources, components, and all the back-office framework remained unchanged. To use XML data comfortably with this object, you need an XMLDOM reference. You can modify the code in Listing 1 to create a general-purpose function that returns an XMLDOM whenever available. Instead of using the method get_responseText, use get_responseXML: pXmlHttp->get_responseXML(
At this point, you can use the XMLDOM hierarchy of nodes to extract the data you need. A more complex application uses a Web server and its set of databases to serve XML data about training classes and seminars through an ASP page (see Figure 4). The application sends HTTP GET commands to specify the ID of the selected class and the various topics and subtopics covered in the class. The application exploits a variant of the code in Listing 1 to retrieve an XMLDOM pointer and walks through the XML tree. When you have a viewer like this one at your disposal, you can ship it and let the application go to the Web to get data. How is all this different from having a WebBrowser embedded in a dialog? It's different in the same way that HTML is different from XML. The former is a terrible mixture of graphics, layout directives, and data. The latter is only a description of data that an application is responsible for displaying. XmlHttpRequest is a great solution for direct Web connectivity from a C++ or script application. It's an object model for HTTP commands that integrates delightfully with the XMLDOM hierarchy. Like anything else, of course, this object has its limitations. Neither XmlHttpRequest nor XMLDOM are useful from within an ASP server-side context (see the sidebar, "Beware of These Server-Side Woes"). And, when you're executing code in an ASP page, and attempt to invoke XmlHttpRequest or try to initialize the XMLDOM with HTTP-based content, you'll experience some security and performance limitations. ASP is implemented as an ISAPI module that runs under the supervision of MTS and IIS. The ASP ISAPI system runs in a protected server service that ends up breaking some of the basic functionality of the WinInet API on which the HTTP capabilities of MSXML are based. At the moment, there's no easy and practical workaround. According to all reports, this problem has been fixed in the yet-to-come version 3.0 of MSXML. Until that version ships, you need a hand-crafted version of XmlHttpRequest that doesn't rely on WinInet and UrlMon and utilizes a raw socket-based mechanism to arrange the server-to-server communication over HTTP. Dino Esposito is a freelance trainer and consultant based in Rome. He authored Visual C++ Windows Shell Programming and Windows Script Host Programmer's Reference for Wrox Press and is a regular contributor to MSDN. E-mail Dino at dinoe@wrox.com 如果您希望与本文章的作者或其所在机构,进一步交流,请联系:姜小姐 jill.jiang@amt.com.cn | 021-51096826-112 | 在线联系 |
CIO职场,强者生存?在2008年,我们将继续看到CIO向商业运营方向发展。与此同时,我们也会看到商业管理人员将与技术管理人员一起竞争CIO岗位。 IT领导者的就职机会虽有不少,但其难度将会大幅提高。2…… 防震减灾,IT当关今天,任何的防震救灾体系,都离不开IT技术。地震观测台是数字化的,震害防御需要对以往的地震信息进行数据分析,应急救援要需要现代多样化的通讯技术。如果说,在许多行业,信息技术还只是一…… |
|
|