SOAP and WSDL interaction?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

SOAP and WSDL interaction?

Post by rullbandspelare » Sun Oct 19, 2008 5:49 am

Hi!
I am trying to interact with a WSDL web service.
Is there anyone who have used MS with webservices like this?
I am trying to send a request with HTTPrequest, but I am not sure that it works. Is there anyway to change the HTTP header

My plan is to use cURL if HTTPrequest does not work.

Code: Select all

curl -H "Content-Type: text/xml; charset=utf-8" \     -H "SOAPAction:" \     -d @soap.txt \     -X POST http://localhost:18181/httpWSDLService/httpWSDLPort

Thanks for ANY input.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Oct 19, 2008 12:42 pm

You can do it with VBScript and MSSOAP.SOAPClient

http://msdn.microsoft.com/en-us/library/ms997641.aspx

Code: Select all

VBSTART

Sub SoapExample
 dim SOAPClient
 set SOAPClient = createobject("MSSOAP.SOAPClient")
 on error resume next
 SOAPClient.mssoapinit("http://www.xmethods.net/sd/CurrencyExchangeService.wsdl")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
 MsgBox SOAPClient.getRate("England","Japan")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
End Sub
VBEND

VBRun>SoapExample
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Sun Oct 19, 2008 9:44 pm

Cool!
Thanks for the very helpful answer!

The URL in the example did not work. But this one does

Code: Select all

VBSTART
Sub SoapExample
 dim SOAPClient
 set SOAPClient = createobject("MSSOAP.SOAPClient")
 on error resume next
 SOAPClient.mssoapinit("http://webservices.daehosting.com/services/eleventest.wso?wsdl")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
 	 Msgbox SOAPClient.StripToNumeric("1a2b3c4d5e6")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
End Sub
VBEND

VBRun>SoapExample
I looked a bit in the forum but I am still not able to input/output variables from the VBscript.

This is what I would like to do. Input %IN% to the VBscript and output %OUT%

Code: Select all

Let>IN=1a2b3c4d5e6

VBSTART
Sub SoapExample
 dim SOAPClient
 set SOAPClient = createobject("MSSOAP.SOAPClient")
 on error resume next
 SOAPClient.mssoapinit("http://webservices.daehosting.com/services/eleventest.wso?wsdl")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
 	 OUT = SOAPClient.StripToNumeric(%IN%)
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
End Sub
VBEND
VBRun>SoapExample

MessageModal>%OUT%

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Oct 20, 2008 8:15 am

Make it a function. Pass the value into the function and set the function's result to the output:

Code: Select all

VBSTART
Function SoapExample(IN)
 dim SOAPClient
 set SOAPClient = createobject("MSSOAP.SOAPClient")
 on error resume next
 SOAPClient.mssoapinit("http://webservices.daehosting.com/services/eleventest.wso?wsdl")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
 SoapExample = SOAPClient.StripToNumeric(IN)
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
End Function
VBEND

VBEval>SoapExample("1a2b3c4d5e6"),OUT
MessageModal>OUT
And you may also want to remove those MsgBox lines and instead have the function return the error codes.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Mon Oct 20, 2008 9:00 am

As always: Super fast support!
This support alone is worth the price of MachroScheduler

But....
The example is not working.

Compilation error Microsoft VBScript :1010
Identifier expected
Line 2, Column 21

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Oct 20, 2008 9:41 am

Ah. IN is a reserved word. So we need to use a different variable name. Try:

Code: Select all

VBSTART
Function SoapExample(input)
 dim SOAPClient
 set SOAPClient = createobject("MSSOAP.SOAPClient")
 on error resume next
 SOAPClient.mssoapinit("http://webservices.daehosting.com/services/eleventest.wso?wsdl")
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
 SoapExample = SOAPClient.StripToNumeric(input)
 if err then
    MsgBox SOAPClient.faultString
    MsgBox SOAPClient.detail
 end if
End Function
VBEND

VBEval>SoapExample("1a2b3c4d5e6"),OUT
MessageModal>OUT
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 22, 2008 1:57 pm

Just discovered that MSSOAP.SOAPClient does not work under Vista and is no longer supported by Microsoft. Instead you need to use the MsXml2.XMLHTTP and MsXml2.DOMDocument objects. It's a little more complicated, but once you've seen one example the next is easier. First construct the XML, then post it to the service, then parse the XML response:

Code: Select all

VBSTART

Function StripToNumeric(sNumber)
    Dim objHttp
    Dim objXml
    Dim tEnvelope
    Dim tResult

    ' Preparation of SOAP header.
    tEnvelope = "<?xml version=""1.0"" ?>"
    tEnvelope = tEnvelope & "<soap:Envelope "
    tEnvelope = tEnvelope & "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" "
    tEnvelope = tEnvelope & "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" "
    tEnvelope = tEnvelope & "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
    tEnvelope = tEnvelope & "<soap:Body>"
    ' Here I define the operation to call
    tEnvelope = tEnvelope & "<StripToNumeric xmlns=""http://webservices.DataAccess.Net/ElevenTest"">"
    tEnvelope = tEnvelope & "<sNumber>" & sNumber & "</sNumber>"
    tEnvelope = tEnvelope & "</StripToNumeric>"
    tEnvelope = tEnvelope & "</soap:Body>"
    tEnvelope = tEnvelope & "</soap:Envelope>"

    ' Create Object of MtEnvelope2.XMLHTTP
    Set objHttp = CreateObject("MsXml2.XMLHTTP")
    Set objXml = CreateObject("MsXml2.DOMDocument")

    ' Load the header as XML
    objXml.loadXML tEnvelope
    ' Open the web service location
    objHttp.open "POST","http://webservices.daehosting.com/services/eleventest.wso", False
    ' Add the SOAPAction header
    objHttp.setRequestHeader "SOAPAction", "StripToNumeric"
    ' We are working with XML
    objHttp.setRequestHeader "Content-Type", "text/xml"
    ' Send the SOAP Message
    objHttp.send objXml.xml

    ' responseText property contains the full answer received from the server
    ' wscript.echo(objHttp.responseText)

    ' Treat the response as XML
    objXml.LoadXml objHttp.responseText
    ' What I want is the returning value, contained in the tag value. I get it and use it
    Set objNodeList = objXml.selectNodes("//soap:Envelope/soap:Body/m:StripToNumericResponse/m:StripToNumericResult")
    If objNodeList.length > 0 Then
        ' If there is at least one node named "valid", get the answer.
        ' Otherwise there are errors, and the web service should have sent a fault message.
      tResult = objXml.selectSingleNode("//soap:Envelope/soap:Body/m:StripToNumericResponse/m:StripToNumericResult").text
    End If

    ' Clean up objects
    Set objXml = Nothing
    Set objHttp = Nothing

    ' Return the result
    StripToNumeric = tResult
End Function

VBEND

VBEval>StripToNumeric("1a2b3c4d5e6"),response
MessageModal>response
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Fri Oct 24, 2008 11:19 pm

This last example worked best for my purposes.
Thanks!

I have one ,yeah.. sure :roll: , last question.

What if the result in the XML was presented like:

Code: Select all

StripToNumericResult  Numbers="123456" Characters="abcde" />

instead of
StripToNumericResult>string
as it is now

The objXml.selectNodes does not work then.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Oct 25, 2008 6:53 am

First, I'm not an XML expert so I may be wrong, but in your example Numbers and Characters are attributes not values. A function would return values. If a Soap function returned more than one it would do so like this:


1234
axdf
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Sat Oct 25, 2008 12:56 pm

I for sure am no XML expert either.
But in my case the returned result realy is presented in this way in the XML answer.
I have tried some variations like, objXml.getAttribute("value") ,to extract the "123456" from:
StripToNumericResult Numbers="123456" Characters="abcde" />

I am not very good at VB syntax and this i guess, is more of a XML VBscript issue than Machroscheduler issue

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Oct 26, 2008 9:08 am

It's an MS XML thing. Need to check out the docs at microsoft.com ...

Here:
http://msdn.microsoft.com/en-us/library ... butes.aspx

So we can use the attributes property of the node. Something like:

xmlNode = objXml.selectSingleNode("//soap:Envelope/soap:Body/m:StripToNumericResponse/m:StripToNumericResult")

sNumbers = xmlNode.Attributes["Numbers"].Value
sChars = xmlNode.Attributes["Characters"].Value
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Tue Oct 28, 2008 7:14 pm

It did not work. Complaints of the [ and ] . canged to () wich gave some other errors.

Instead i dug it out of the complete XML answer:

Code: Select all

Separate>%response%,StripToNumericResult Numbers=",ID
pos>",ID_2,1,pos
Let>pos=%pos%-1
midstr>%ID_2%,1,%pos%,Numbers

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts