How you consume a Web Service depends on your situation. Recall from yesterday that you can access Web Services with three different protocols: Http-Get, Http-Post, and SOAP. The first two are the easiest to implement because they only require the URL of the Web Service. All of today's examples have used SOAP, which provides the richest methods for consuming services.
There's one drawback to using SOAP, however. As you may have noticed, encoding the commands and data in SOAP messages is rather complex (although creating the proxies that contain the methods to do so is not). The messages sent back and forth are quite large because SOAP must send XML schema information. Depending on the type of connection you or your clients have to the Internet, this can affect performance.
Another method of consuming a Web Service is Http-Get. If you know the URL of the service and the method(s) and parameter(s) it expects, you can invoke the service's methods. For example, you can access the following URL to consume the Add method from the Calculator service you created yesterday:
Put a forward slash and the method name after the URL, followed with the parameters in a querystring. For example, the URL of the service is
The suffix, containing the method name and parameters, is
When requested, this URL outputs the XML containing the output from the method. In this case, you see the following:
<?xml version="1.0"?> <int xmlns="http://tempuri.org/">3</int>
You can also consume a service via an Http-Post operation. Simply set the action attribute of an HTML form to the Add method:
This is the URL followed by a slash and the method name. When you submit the form, any parameters will be posted to the service and the appropriate XML output will be generated. Just remember that the form parameters must have the same names as the parameters expected by the service.
So which of these methods should you use? The choice is simple, really. When you require complex data to be sent back from the service, such as a DataSet or a class, use SOAP. The other two methods can't send this type of data.
If you don't require complex data, and your application or clients may be bandwidth-sensitive, use the Http-Get or Http-Post method. They send less data back and forth, resulting in better performance.
|D0 ||Don't |
|Do use SOAP when you think you'll need complex data or rich programmatic access to your service. ||Don't use SOAP when bandwidth is a critical factor. Use Http-Get or Http-Post instead. |