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.
This web service is listed on XMethods
here.
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:
<getJoke xmlns=""http://interpressfact.net/webservices/"">" ... the required parameter category in the DOM:
<Category>category </Category> ... and last but not least, setting the headers for method invocation:
- Setting request as the SOAP request for POST
- Assigning a HOST for SoapServer
- Setting the content-type as text/XML
- Encoding the request into utf-8 format
- Setting the SOAPAction for the invocation URI
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) What we receive as XML is:
<?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:
<%' [ Interpressfact 2002 ]
Function getJoke(category)
Const SoapServer = "interpressfact.net"
Const SoapPath = "/webservices/getJoke.asmx"
Dim xmlhttp, strSoap
set xmlhttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
'Create Soap request
strSoap = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
"<soap:Body>" & _
"<getJoke xmlns=""http://interpressfact.net/webservices/"">" & _
"<Category>" & category & "</Category>" & _
"</getJoke>" & _
"</soap:Body>" & _
"</soap:Envelope>"
'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.
Next: Implementation in .NET Using System.Net.WebClient >>
More ASP Articles
More By Adnan Masood