Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WPF

Consuming a Web Service .NET with VB6.0 App

Rate me:
Please Sign up or sign in to vote.
4.96/5 (14 votes)
25 Apr 2017CPOL4 min read 116.8K   7.3K   28   20
I will show you how to get a response from a Web Service developed over .NET (version does not matter)

Image 1

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.

ConsumingWebService/referencias.JPG

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:

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

Private Sub btnUrl_Click()
  frmUrl.Show
End Sub
Private Sub btnSoap_Click()
  frmSoapAction.Show
End Sub
Private Sub btnXml_Click()
  frmXMLPeticion.Show
End Sub
...

ConsumingWebService/help_buttons.JPG

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.:

VB.NET
''
''
Private Sub cmdRequest_Click()
Dim oWsXML As New XMLRequestNuic
''initialize Class
Dim aDatos() As String
''Variable to store the parameters to pass to the 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)>:

VB.NET
''
'' 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: 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)      '' We Assign the Max index to the iTotalElem Variable.

ConsumingWebService/InputValues.JPG

Image: #2

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

VB.NET
''
'' 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
Else
bFlag = 0
End If
Else
bFlag = 0
End IfElse
bFlag = 0End If

ConsumingWebService/XMLrequest.JPG

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.

VB.NET
''
'' 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
  ''We store in a Temporary variable the XmlSoap.
  tmpXmlSoap = txtXmlSoap.Text ' Replace(txtXmlSoap.Text, vbCrLf, "")
  iCont = 1
  		For i = 1 To Len(txtXmlSoap.Text)
		  If InStr(tmpXmlSoap, "string") Then
			     ''We Store the first coincidence for the string 
                                ''word "STRING"
			     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
		Next
    ''We Assign the resulto to 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.

VB.NET
'''
''
'''
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
 ''once validated all, let's begin with the request to the WebService!!!
 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.

VB.NET
''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")
' Load the XML
objDom.async = False
objDom.LoadXml XmlBody
'Open the connection to the Web Service
objXmlHttp.open "POST", AsmxUrl, False
' Create the Headers fo the XML
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "SOAPAction", SoapActionUrl
' Send the commands to request the XML response
objXmlHttp.send objDom.xml
' The result is stored in a variable called strRet
strRet = objXmlHttp.responseText
frmMain.txtWsResponse.Text = strRet
' We close the object.
Set objXmlHttp = Nothing
' we Extract the results with a Cycle
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.

License

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


Written By
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 applications And others langauges such like: VB6, VB.NET 2003,2005,2010, C Sharp, AJAX, ASP 3.0, Javascript AND XML. and SQL SERVER 2000, SQL 2008,SQL 2012, WCF, WPF,MVC

Comments and Discussions

 
QuestionHow manage Header Authentication? Pin
Member 792126826-Feb-21 22:18
Member 792126826-Feb-21 22:18 
QuestionWhy he don't find that? Pin
LeMarS16-Aug-19 13:04
LeMarS16-Aug-19 13:04 
AnswerRe: Why he don't find that? Pin
Rene Bustos11-Jan-23 6:22
Rene Bustos11-Jan-23 6:22 
QuestionHow I can do conexion? Pin
LeMarS7-Aug-19 10:42
LeMarS7-Aug-19 10:42 
QuestionComo hago la conexion?? Pin
LeMarS5-Aug-19 18:59
LeMarS5-Aug-19 18:59 
AnswerRe: Como hago la conexion?? Pin
Nelek5-Aug-19 19:02
protectorNelek5-Aug-19 19:02 
AnswerRe: Como hago la conexion?? Pin
Kornfeld Eliyahu Peter5-Aug-19 19:53
professionalKornfeld Eliyahu Peter5-Aug-19 19:53 
QuestionThis example work on vs2017/2019? Pin
Member 141281337-Jun-19 21:03
Member 141281337-Jun-19 21:03 
AnswerRe: This example work on vs2017/2019? Pin
Rene Bustos1-Aug-19 4:18
Rene Bustos1-Aug-19 4:18 
Questionserver did not recognize the value of http header soapaction Pin
mmateoquiroz6-Dec-18 6:20
mmateoquiroz6-Dec-18 6:20 
AnswerRe: server did not recognize the value of http header soapaction Pin
Rene Bustos15-Feb-19 9:41
Rene Bustos15-Feb-19 9:41 
QuestionConsuming a Web Service .NET with VB6.0 App Pin
Member 1001278830-Jun-16 8:57
Member 1001278830-Jun-16 8:57 
AnswerRe: Consuming a Web Service .NET with VB6.0 App Pin
Rene Bustos20-Jul-16 10:01
Rene Bustos20-Jul-16 10:01 
Questionwcf FROM vb6 Pin
DiegoGalindoSaeta22-Apr-15 23:20
DiegoGalindoSaeta22-Apr-15 23:20 
AnswerRe: wcf FROM vb6 Pin
Rene Bustos1-Aug-19 4:23
Rene Bustos1-Aug-19 4:23 
Hi DiegoGalindoSaeta

See the response to
Member 14128133


Have a nice day friend!
QuestionFunciono perfecto Pin
jjbara10-Jul-14 12:08
jjbara10-Jul-14 12:08 
AnswerRe: Funciono perfecto Pin
Rene Bustos29-Dec-14 5:14
Rene Bustos29-Dec-14 5:14 
Questionwhat about ssl Pin
niyo6611-Aug-13 21:31
niyo6611-Aug-13 21:31 
AnswerRe: what about ssl Pin
Rene Bustos28-Nov-13 4:06
Rene Bustos28-Nov-13 4:06 
GeneralMy vote of 5 Pin
LuisFEL23-Nov-11 8:32
LuisFEL23-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.