Click here to Skip to main content
13,093,944 members (55,618 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 18 Aug 2011

Consuming a Web Service .NET with VB6.0 App

, 18 Aug 2011
Rate this:
Please Sign up or sign in to vote.
I will show you how to get a response from a Web Service developed over .NET (version does not matter)
This is an old version of the currently published article.


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!


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:

'' These are the help buttons only for breaking the ice, like a Mexican says. (image: #1)

Private Sub btnUrl_Click()
End Sub
Private Sub btnSoap_Click()
End Sub
Private Sub btnXml_Click()
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 
''Iniciamos la clase que cosulta al bin    
Dim aDatos() As String           
''Variable para almacenar los parametros que se pasaran al web service    
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)>:

'' In this section, we apply the SPLIT function for create an array of data.
'' Remember. we are still validating the inputs values and XML Structure
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: El formato para los parametros debe ser: 
Dato1,dato2, o Dato1-dato2 o Dato1.Dato2. o Dato1+Dato2+": Exit Sub        
End If    
End If
End If
iTotalElem = UBound(aDatos)      '' Asignamos el maximo index a la variable iTotalElem


Image: #2

The next step is to validate if the XML structure has the minimum tags:

'' you can Add more tags to this section (Image #3 )
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        
bFlag = 0        
End If    
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.

'' Replace word "string" for "@Parametro1" for the first coincidence 
'' and "@Parametro2" for the second coincidence of the "string" word.
''If chkString.Value = 1 Then  Dim iInicio As Integer
  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
  ''Almacenamos en variable temporal
  tmpXmlSoap = txtXmlSoap.Text ' Replace(txtXmlSoap.Text, vbCrLf, "")
  iCont = 1
  		For i = 1 To Len(txtXmlSoap.Text)
		  If InStr(tmpXmlSoap, "string") Then
			     ''Almacenamos el inicio
			     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  ''Plus 6, because we need begin to the endo of 
                                            ''STRING word, to find the next position
			     iCont = iCont + 1
		  End If
    ''Asignamos el resultado al txtXmlSoap.text
    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
 ''Una vez Validado todo, empezamos con el proceso de consulta al VinPower
 If txtUrl.Text = "" Or txtSoapAction.Text = "" Or txtXmlSoap.Text = "" Then
     MsgBox("Error: Favor de revisar los datos como: URL, Soap Action, XML")
     Exit Sub
     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.

''This is the Class code XMLrequestNuic
''we create the objects from MSXML2
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")
' Carga el XML
objDom.async = False
objDom.LoadXml XmlBody
'abre conexion con el Web Service "POST", AsmxUrl, False
' crea los encabezados del XML
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "SOAPAction", SoapActionUrl
' Envia el comando de apertura XML
objXmlHttp.send objDom.xml
' Se trae todo lo resultante de la peticion (en bruto)
strRet = objXmlHttp.responseText
frmMain.txtWsResponse.Text = strRet
' Se cierra el objeto
Set objXmlHttp = Nothing
' Extraemos los resultados con un ciclo
' Extract the results in a Cycle
bDatos = Split(Criterios, ",")
For Each Dato In bDatos
	If iCont = 1 Then
		sCriterios = buscarXML(Dato, strRet)
		sCriterios = sCriterios & "," & buscarXML(Dato, strRet)
	End If
	iCont = iCont + 1
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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rene Bustos
Systems Engineer National Unity Insurance Co.
Mexico Mexico
Hi people!,I am Rene bustos, im from Mexico, Actually i work for an American company Called National Unity insurance Company. this company is dedicated to insure Commercial Vehicles, Personal Vehicles, home owners.
Since my 16 years i had interest for the "how they do that" and "how can they make that circle how!!"
then every day i made a lot o questions to my father about the computers and he can not answer me at all. so since that day so far my research about "how can i develop an application" is over my mind.
Well Right now i develop Web applications or Web Services with VB.NET. i made application And others langauges such like: VB6, VB.NET 2003,2005,2010, C Sharp, AJAX, ASP 3.0, Javascript AND XML. and SQL SERVER 2000, AND SQL 2008

You may also be interested in...


Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
QuestionConsuming a Web Service .NET with VB6.0 App Pin
Member 1001278830-Jun-16 8:57
memberMember 1001278830-Jun-16 8:57 
AnswerRe: Consuming a Web Service .NET with VB6.0 App Pin
Rene Bustos20-Jul-16 10:01
memberRene Bustos20-Jul-16 10:01 
Questionwcf FROM vb6 Pin
DiegoGalindoSaeta22-Apr-15 23:20
memberDiegoGalindoSaeta22-Apr-15 23:20 
QuestionFunciono perfecto Pin
jjbara10-Jul-14 12:08
memberjjbara10-Jul-14 12:08 
AnswerRe: Funciono perfecto Pin
Rene Bustos29-Dec-14 5:14
memberRene Bustos29-Dec-14 5:14 
Questionwhat about ssl Pin
niyo6611-Aug-13 21:31
memberniyo6611-Aug-13 21:31 
AnswerRe: what about ssl Pin
Rene Bustos28-Nov-13 4:06
memberRene Bustos28-Nov-13 4:06 
GeneralMy vote of 5 Pin
LuisFEL23-Nov-11 8:32
memberLuisFEL23-Nov-11 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 18 Aug 2011
Article Copyright 2011 by Rene Bustos
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid