Consuming a Web Service in Managed C

Consuming a Web Service in Managed C++

Writing the code to make the HTTP request, packaging up the parameters, and parsing the XML would not be hard. But when you write an application in Visual C++, consuming a Web Service is actually even simpler.

The concept of adding references in managed C++ is extended to include adding a Web reference. This is a reference to a Web Service. When you add it, a proxy class is generated that has one method for each Web method in the Web Service. Your code calls methods of this proxy class, and the calls are handled by the proxy class, which packages the parameters, makes the HTTP request, and parses the result XML for you.

Follow these steps to create a Visual C++ application that uses the Web Service created earlier in this chapter. First, create a Windows Forms Application called UseUtilities . Drag two text boxes and a button onto the form, grouped together in the top half. Change the name of the first text box to intText and remove the default text. Change the name of the second text box to doubleText and remove the default text. Change both the name and the text of the button to Factor . Drag a date time picker and a button onto the bottom half of the form. Leave the name of the date time picker as dateTimePicker1 . Change the name and the text of the button to Weekend . The final interface should resemble Figure 10.2.

Figure 10.2. The client user interface tests both Web Services.

graphics/10fig02.gif

Double-click the Factor button to edit the handler code. This handler will call the Web Service, and to do so requires a reference to the Web Service proxy. Right-click the References node in Solution Explorer, and choose Add Web Reference. In the Add Web Reference dialog box, fill in the text box labeled URL with the URL to the .asmx file of the Web Service created earlier in this chapter:

 
 http://localhost/Utilities/Utilities.asmx 

Click the Go button and the Web Reference dialog box shows the same list of services the browser displays when you load this URL yourself. Change the Web Reference Name from localhost to Utilities and click Add Reference. Visual Studio will generate code for you, and you will see another node in Solution Explorer labeled Utilities, along with an entry, Utilities.h, under Generated Files.

Calling the Web Service consists of creating an instance of the proxy class, and then calling a method. Add these lines to the click handler for the Factor button:

 
 private: System::Void Factor_Click(System::Object *  sender,                                    System::EventArgs *  e) {     Utilities::UtilitiesClass* ws = new Utilities::UtilitiesClass();     int i = intText->Text->ToInt32(NULL);     double d = doubleText->Text->ToDouble(NULL);     if (ws->Factor(i,d))         MessageBox::Show(S" The double is a factor");     else         MessageBox::Show(S" The double is not a factor"); } 

The first line of code creates the proxy object. Then the numbers are extracted from the text boxes. The ToInt32() and ToDouble() methods both take a pointer to an IFormatProvider , because numerical formats can vary. For example, the number written as 1.234 in North America is written as 1,234 in Europe. A format provider assists the conversion process by holding format rules such as the currency symbol, the punctuation symbol that indicates the decimal point, and so on. Passing NULL to these functions instructs them to use the format provider set up on the machine where the code is running.

Build and run this code, and enter some combinations of integer and double that should return true (for example, 2.5 is a factor of 5) and others that should return false (for example, 3.1 is not a factor of 5). Ensure you get the expected behavior.

Working with a DateTime object is no harder than working with integer and double variables . Switch back to a design view of Form1.h and double-click the Weekend button. Enter this code:

 
 private: System::Void Weekend_Click(System::Object *  sender,                                     System::EventArgs *  e) {     Utilities::UtilitiesClass* ws = new Utilities::UtilitiesClass();     DateTime dt = dateTimePicker1->Value;     if (ws->Weekend(dt))         MessageBox::Show("It's the weekend!");     else         MessageBox::Show("Not the weekend yet."); } 

This code creates an instance of the proxy object, and passes the value from the date time picker to the Weekend() method.

Describing Web Services with WSDL

If you type the click handler code yourself, you'll notice the IntelliSense helping you to choose the Web methods of the Utilities Web Service and reminding you of the parameters the methods take. This information comes from the WSDL (Web Services Description Language, pronounced wiz-dull) that describes the Web Service itself. To see the WSDL for this chapter's sample Web Service, bring up a browser and point it to the Web Service .asmx file again, and then click the Service Description link. You'll see XML describing the Web Service in a machine-readable way that is, with a little effort, also human-readable . For example, try to read this excerpt:

 
 <s:element name="Factor"> <s:complexType> <s:sequence>   <s:element minOccurs="1"  maxOccurs="1"  name="i"  type="s:int"  />   <s:element minOccurs="1"  maxOccurs="1"  name="d"  type="s:double"  />   </s:sequence>   </s:complexType>   </s:element> <s:element name="FactorResponse"> <s:complexType> <s:sequence>   <s:element minOccurs="1"  maxOccurs="1"  name="FactorResult"          type=" s:boolean"  />   </s:sequence>   </s:complexType>   </s:element> 

The first element describes the parameters for a call to Factor . There is to be exactly one element ( minOccurs is 1 and maxOccurs is 1 ) called i , of type int . There is to be exactly one element called d , of type double .

The second element, FactorResponse , describes the return value from the Web method. It is of type boolean .

The WSDL for the Utilities method goes on to describe the Weekend element and then goes through the SOAP required to make the same calls. It's all there, suitable for use by any development tool (or hardworking developer) to ensure that the parameters passed to and returned from the Web methods are the expected types.

There's no need to create XML, parse XML, understand SOAP, or even know what WSDL is to use a Web Service. It's all handled for you behind the scenes.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

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