Introduction
Web services are an important part in the management of information.
That's why although information technology continues to advance, we must not forget the past for some time.
Consuming Web Services with the same technology that was developed ... is easy. but what if we need to consume that web service with another technology such as past Visual Basic 6.0. Still need to do it? Sure. This is why I present this humble article on how we can consume a web service developed in. NET with an application developed in Visual Basic 6.0.
Let's begin. Good luck people!
Background
You may know a little bit of XML and Optional Installed SOAP TOOL KIT 3.0 or later (in case you have problems with MSXML2.DOMDocument
), this can be found in Microsoft official site or Google: SOAP TOOLKIT 3.0 DOWNLOAD :D , but I was thinking... if MSXML2 library comes with Internet Explorer 8?
For the Web Service: VS 2008 or VS 2010.
Using the Code
We will start.
In this draft VB 6.0, we have 4 forms and a class called XMLRequestNuic
. XMLRequestNuic
class is the one responsible for making the request to Web Service with the body of the XML to send. Remember that there are many methods to query a Web Service, but this way is more transparent and understandable to a developer.
The first form called frmMain
is the one that stores the user interface and other forms only store pictures for help.
I will explain in detail how to build the XML request and how to get the answer in simple steps.
First, we see the simple parts and then we will increase the level.
Consider the following code snippet:
Private Sub btnUrl_Click()
frmUrl.Show
End Sub
Private Sub btnSoap_Click()
frmSoapAction.Show
End Sub
Private Sub btnXml_Click()
frmXMLPeticion.Show
End Sub
...
Image: #1
The following code fragment initializes the class XMLrequestNuic
. This class will detail below, do not worry.
Then, declare an array variable called <aDatos()>
which will store the query values??
We also have a variable called <iTotalElem>
which will serve to store the amount of submitted values to the Web Service.
We also have a Boolean variable that is used to validate if the XML structure is correct.
Then we will use variable iCant
to build cycle-type variables <@ parameter1>
, <@ parameter2>
.. etc.:
Private Sub cmdRequest_Click()
Dim oWsXML As New XMLRequestNuic
Dim aDatos() As String
Dim iTotalElem As Integer
Dim bFlag As Integer
Dim iCant As Integer
iCant = 1
bFlag = 0
In the following code snippet, what we do is apply the SPLIT function () txtCriterios.Text
to the field, this field is where you type txtCriterios.Text
the values we want to send to the web service.
To perform the split ()
is that the values are separated by any of the following limitations: "," or "-" or "." or "+", of course if you want to add more delimiters, you can. Continue with the explanation, when we do the split ()
. Then store the result in variable <aDatos()>
and then assign the number of elements to the variable: <iTotalElem=UBOUND(aDatos)>
:
aDatos = Split(txtCriterios.Text, ",")If Not IsArray(aDatos) Then
aDatos = Split(txtCriterios.Text, "-")
If Not IsArray(aDatos) Then
aDatos = Split(txtCriterios.Text, ".")
If Not IsArray(aDatos) Then
aDatos = Split(txtCriterios.Text, "+")
If Not IsArray(aDatos) Then MsgBox "Error: The parameters format should be:
Dato1,dato2, o Dato1-dato2 o Dato1.Dato2. o Dato1+Dato2+": Exit Sub
End If
End If
End If
iTotalElem = UBound(aDatos)
Image: #2
The next step is to validate if the XML structure has the minimum tags:
If InStr(txtXmlSoap.Text, "<?xml") > 0 And InStr(txtXmlSoap.Text, "<?xml") <= 6 Then
bFlag = 1
If InStr(txtXmlSoap.Text, "<soap:Envelope") > 0 Then
bFlag = 1
If InStr(txtXmlSoap.Text, "<soap:Body>") > 0 Then
bFlag = 1
Else
bFlag = 0
End If
Else
bFlag = 0
End IfElse
bFlag = 0End If
Image #3
The following fragment of code starts with the creation of the input parameters, but we have to create the correct format for each query value, i.e., we need to create variables as follows: <@ parametro1<code><code>>
, @ parametro2, @parametro3 @>. This depends on the amount of data that the Web Service needs to return the result.
To do this, it is necessary that the value of chkString.Value = 1
for the application. Look for the words "string
", if you omit this checkbox then we will have to manually <@ Parametro (NumberOfInputValue)>
every word "String
" of XML.
In this case. we do this:
We create a cycle which searches for the word "String
" and replaces it with "@ parametro1
" and if you find a second word "string
", then replaces it with "@ Parametro2
" and so on. The end is the XML home ... but with the words of @ parameter1
instead of the first word "string
" found and @ parameter2
instead of the second word "string
" found in the XML.
REMEMBER: You can add more data type like INTEGER
, BOOLEAN
, Float
, etc. but remember that a Web Service if you don't pass a value when a data type is different from STRING
type... it will raise an Error. This is why I only put examples with STRING
type.
Dim iFinalParte1 As Integer
Dim iInicioParte2 As Integer
Dim iFinal As Integer
Dim LongURL As Integer
Dim oFuncion() As String
Dim sBuscar As String
Dim tmpUrlSoap As String
Dim iCont As Integer
Dim tmpXmlSoap As String
Dim tmpParte1 As String
Dim tmpParte2 As String
tmpXmlSoap = txtXmlSoap.Text
iCont = 1
For i = 1 To Len(txtXmlSoap.Text)
If InStr(tmpXmlSoap, "string") Then
iFinalParte1 = InStr(txtXmlSoap.Text, "string")
iInicioParte2 = InStr(txtXmlSoap.Text, "string") + 6
tmpParte1 = Mid(tmpXmlSoap, 1, iFinalParte1 - 1)
txtXmlSoap.Text = tmpParte1
tmpParte2 = Mid(tmpXmlSoap, _
iInicioParte2, Len(tmpXmlSoap))
txtXmlSoap.Text = tmpParte2
tmpXmlSoap = tmpParte1 & "@Parametro" & iCont & tmpParte2
txtXmlSoap.Text = tmpXmlSoap
i = i + 6
iCont = iCont + 1
End If
Next
txtXmlSoap.Text = tmpXmlSoapEnd If
The following code snippet now replaces "@parametro1
" with the first value that you type in the field txtCriterios.text
, and so on.
Search for @Parametro
& iCant
, if is found then we proceed with the replace function.
For Each oParametro In aDatos
Dim Var As String
If InStr(txtXmlSoap.Text, "@Parametro" & iCant) > 0 Then
txtXmlSoap.Text = Replace(txtXmlSoap.Text, "@Parametro" & iCant, oParametro)
End If
iCant = iCant + 1
Next
If txtUrl.Text = "" Or txtSoapAction.Text = "" Or txtXmlSoap.Text = "" Then
MsgBox("Error: Favor de revisar los datos como: URL, Soap Action, XML")
Exit Sub
Else
txtResult.Text = oWsXML.PostWebservice_
(txtCampos.Text, txtUrl.Text, txtSoapAction.Text, txtXmlSoap.Text)
End If
Now, let's see the last step... the CORE of this project: XMLrequestNuic
class. This class is where our XML body takes form and gets the data From the Web Service.
Basically, the class made the request through the Msxml2.XMLHTTP
object which is responsible to return an XML with all the fields and values, we only read that result and obtain the values of the fields we need.
Public Function PostWebservice(ByVal Criterios As String, _
ByVal AsmxUrl As String, ByVal SoapActionUrl As String, ByVal XmlBody As String) As String
Set objDom = CreateObject("MSXML2.DOMDocument")
Set objXmlHttp = CreateObject("MSXML2.XMLHTTP")
objDom.async = False
objDom.LoadXml XmlBody
objXmlHttp.open "POST", AsmxUrl, False
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "SOAPAction", SoapActionUrl
objXmlHttp.send objDom.xml
strRet = objXmlHttp.responseText
frmMain.txtWsResponse.Text = strRet
Set objXmlHttp = Nothing
bDatos = Split(Criterios, ",")
For Each Dato In bDatos
If iCont = 1 Then
sCriterios = buscarXML(Dato, strRet)
Else
sCriterios = sCriterios & "," & buscarXML(Dato, strRet)
End If
iCont = iCont + 1
Next
intPos1 = InStr(strRet, "Result>") + 7
intPos2 = InStr(strRet, "</")
strRet = sCriterios
If strRet = "" Then
If intPos1 > 7 And intPos2 > 0 Then
strRet = Mid(strRet, intPos1, intPos2 - intPos1)
End If
End If
Points of Interest
As you see, this article was easy. I am assuming that many developers still have little information on how to work with XML structures, which is why in a second installment, I'll update this article to discuss how we can handle XML nodes.
Please provide a "Vote" if this would be helpful.