only for RuBoard |
In Chapter 5, "MSXML Parser," XMLHTTP was briefly introduced. In this section, Listing 11.6 provides an example that consumes the web service directly from a web browser by using XMLHTTP to send and receive the SOAP request and response.
<HTML> <HEAD> <TITLE>XMLHTTP Browser Client for ASP.NET Web Service</TITLE> </HEAD> <BODY onload='makeSOAPCall(4,3)'> </BODY> </HTML> <SCRIPT LANGUAGE=javascript> <!-- function makeSOAPCall( varA ,varB ) { var objXMLHTTP = new ActiveXObject("MSXML2.XMLHTTP") //Specify the URL for the ASP.NET Web Service objXMLHTTP.open("POST", "http://localhost/WebServices/Calculator/Calculator.asmx" , false , "" , "" ); objXMLHTTP.setRequestHeader("SOAPAction","http://tempuri.org/Add" ); objXMLHTTP.setRequestHeader("Content-Type", "text/xml" ); //Create the SOAP Request strSOAPRequest = '<?xml version="1.0" encoding="utf-8" ?>' + '<soap:Envelope' + ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xmlns:xsd="http://www.w3.org/2001/XMLSchema" >' + '<soap:Body>' + '<Add xmlns="http://tempuri.org/">' + '<a>'+ varA +'</a>' + '<b>'+ varB +'</b>' + '</Add>' + '</soap:Body>' + '</soap:Envelope>' alert("=====================SOAP Request====================== \n" + strSOAPRequest) //Send the Request to the .NET Web Service objXMLHTTP.send(strSOAPRequest); alert("=====================SOAP Response===================== \n" + objXMLHTTP.responseXML.xml) //Get the Envelope Node var soapEnvelope = objXMLHTTP.responseXML; //Get the Body Node var soapBody = soapEnvelope.selectSingleNode("//soap:Body"); //Get the Result of the call. alert( soapBody.selectSingleNode("AddResponse/AddResult").text ) } //--> </SCRIPT>
This code assumes that the request is successful. A production code should also expect that there might be exceptions within the code and handle the SOAP faults to show proper messages to the user . Figure 11.11 and Figure 11.12 show the request and response captured through JavaScript alerts.
When you use a SOAP implementation, a proxy generated on the client hides the details of the SOAP request and response formats. But when you deal with raw XML to create SOAP requests for an ASP.NET web service, you can get some help with the SOAP request and response formats by typing the URL for the web service with the ?op= + WebMethod name appended to the URL in a browser. Figure 11.13 shows a sample SOAP request and response.
As mentioned in the Chapter 5, you can also use the ServerXMLHTTP to consume the web service from an ASP page in the same manner as you did in the HTML page.
only for RuBoard |