Methods of Calling Web Services

Summary

Introduction

The primary method of calling a Web Service is sending a SOAP request message via HTTP and receiving a SOAP response message (or fault). However, ASP.NET Web Services also allow two additional means of calling Web Services: HTTP-GET and HTTP-POST.

This means that consumers can access your Web Service via any Web Browser by using MSXML object's XMLHTTP methods to send/receive SOAP messages, or by using an older technology that does not understand SOAP but can send/receive HTTP messages. This ultimately means wider audience.

This section discusses how to call Web Services via HTTP-GET and HTTP-POST, why you would want to allow this type of access, how to exclude it, and what are some of the inherent limitations in using HTTP-GET and HTTP-POST.

Note that any Web Service implementation that relies on SOAP-specific mechanisms such as SOAP headers will not work when called via HTTP-GET and HTTP-POST. Also, if your Web Service allows XML to be passed into it, it might not work well with HTTP-GET and HTTP-POST.

Why would anyone ever want to use anything but SOAP? You might have a trading partner that needs data to be returned in a non-SOAP format, or you may need to interact with someone who is unwilling to learn SOAP (Ageing developers!) or you may have to build a solution that enlists older technologies that can work with HTTP-GET and HTTP-POST.

Calling a Web Service Using HTTP-GET

This is very easy to do. HttpGetPost example creates a very simple Web Service that adds two integers and return the result back to the caller. The Web Service is called via an HTML page as shown below. Note that the name of the parameters in the query string  in the HTML page must be exactly the same as the name of the parameters in the Web Service:

// Web Service: Service1.asmx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace HttpGetPost
{
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //CODEGEN: This call is required by the ASP.NET Web Services Designer
            InitializeComponent();
        }
:
        #region Component Designer generated code
            ... 
        #endregion

        [WebMethod]
        public int AddIntegers( int nNum1, int nNum2 )
        {
            return nNum1 + nNum2;
        }
    }
}

<!-- HTML page that calls a Web Service via HTTP-GET -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <TITLE></TITLE>
    </HEAD>

    <BODY>
        <a href="http://localhost/HttpGetPost/Service1.asmx/AddIntegers?nNum1=10&nNum2=20" >Click here</a> to add two integers via a web service
    </BODY>
</HTML>

When browsing the HTML page in IE, the output looks like. Pay special attention to the query string in the Address text box:

Calling a Web Service Using HTTP-POST

Again calling a Web Service via HTTP-POST is very easy. The same Web Service shown above is called with HTTP-POST as shown below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <TITLE></TITLE>
        <META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
    </HEAD>

    <BODY>
        <form method="post" action="http://localhost/HttpGetPost/Service1.asmx/AddIntegers">
            <p>Number 1: <input id="ID1" type="text" name="nNum1"></p>
            <p>Number 2: <input id="ID2" type="text" name="nNum2"></p>
            <p><input type="submit" value="Add Numbers"></p>
        </form>
    </BODY>
</HTML>

Results of calling this HTML page are shown below. Again, note the URL in the address text boxes:

Calling a Web Service using MSXML XMLHTTP object

MSXML component supports the XMLHttp object that enables you to send and receive XML from any URI that returns XML. After the XML is returned from the Web site, it can be loaded into an XML document  (MSXML DOM) and easily processed. The following shows code from a VB project that shows how to call the Web Service used in the previous two examples. Note that the hard-code URL string can be generated programmaticaly:

dim objXMLDom as MSXML2.DOMDocument
dim oXML as MSXML2.ServerXMLHTTP
dim strURL as string

' Create necessary objects
set oXML = new MSXML2.ServerXMLHTTP
set objXMLDom = new MSXML2.DOMDocument
strURL = "http://localhost/HttpGetPost/Service1.asmx/AddIntegers?nNum1=10&nNum2=20"

' Now send request to the server
oXML.Open "GET", strURL, FALSE
oXML.send

' Now process the response back from the server
dim strResponse as string
strResponse = oXML.responseText
objXMLDom.loadXML strResponse