In part 2 of this article Adnan shows us how to fake a web service using XMLHTTP in ASP, how to handle HTTPS authentication via ServerXMLHTTP and more.
HTTP Tunneling Revealed: Part 2/3 - Invoking a Web Service Using Only XMLHTTP (Page 3 of 5 )
In this article I'm more focused on practical implementation of web services, but for those who needs details, IBM's web services architect overview is a good resource.
XMethods, as its name suggest, is a library for methods implemented via Web Services. "This site is a "virtual laboratory" for developers , listing publicly available web services and showcasing new ways this technology can be applied".
As they say, laughter is the best Medicine!. I have found an interesting web service for jokes by Innerpressfact (http://interpressfact.net/webservices/) that accepts a SOAP envelope with joke category ID in it and returns a random joke.
Now, down to the business! How can we implement this web service using traditional XMLHTTP? As you learned above, SOAP is a protocol to exchange information, period. It has a concrete format to exchange data over the wire. It provides the namespace, the XML schema and the required parameters to invoke the web service and read the resultant string.
The SOAP invocation string comprises of the getjoke name space:
<?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://interpressfact.net/webservices/">Q: How do you get a lawyer out of a tree? A: Cut the rope.</string>
The XMLDOM function, xml.selectSingleNode returns the joke as the end result. The complete listing explaining the invocation is shown below:
'Send Soap Request xmlhttp.Open "POST", "http://" & SoapServer & SoapPath, False xmlhttp.setRequestHeader "Man", "POST " & SoapPath & " HTTP/1.1" xmlhttp.setRequestHeader "Host", SoapServer xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8" xmlhttp.setRequestHeader "SOAPAction", http://interpressfact.net/webservices/getJoke xmlhttp.send(strSoap) If xmlhttp.Status = 200 Then ' Response from server was success getJoke = xmlhttp.responseText Else ' Response from server failed getJoke = ("Temporary Error.") End If set xmlhttp = nothing End Function If request("category") <> "" then Dim Joke dim xml : set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = False 'Load Xml From ResponseText of the getJoke function xml.loadxml(getJoke(request("category"))) ' Select a Single node from ResponseText dim oNode : set oNode = xml.selectSingleNode("soap:Envelope/soap:Body/getJokeResponse/getJokeResult") 'Joke variable now contains the Received Joke from the getJoke webservice Joke = oNode.text set xml = nothing end if %> <h1> Joke from interpressfact </h1> <%=joke%>
You can invoke virtually any SOAP based web service via this mechanism, although the parameters need to be changed of course.