0% found this document useful (0 votes)
13 views11 pages

How... How To Use A Web XML Service From VB6

This document describes how to create a client in Visual Basic 6 to access an XML web service created with .NET. It explains the necessary requirements such as VB6, the Microsoft Internet Transfer control, and a reference to Microsoft XML. It details how to send a SOAP request to the web service with the desired parameters and receive the response. It includes code to create the SOAP request, send it using the Inet control, and process the received XML response. Finally, it shares the complete example code and a screenshot of the form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

How... How To Use A Web XML Service From VB6

This document describes how to create a client in Visual Basic 6 to access an XML web service created with .NET. It explains the necessary requirements such as VB6, the Microsoft Internet Transfer control, and a reference to Microsoft XML. It details how to send a SOAP request to the web service with the desired parameters and receive the response. It includes code to create the SOAP request, send it using the Inet control, and process the received XML response. Finally, it shares the complete example code and a screenshot of the form.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

17/01/2011 How... How to use a Web XM service...

Guille, the Web of Visual Basic, C#, .NET and more...


Lo+-[Link]-How...-Collaborate-VB6-API-HTML-View-Links-
Forums

How... in .NET
Use an XML Web service from
VB6
Published on June 20, 2003
Updated on Jun 29, 2003 (Dec 14, 2003)

Modification to NOT use the Internet Transfer Control to access the service
Web (29/Jun/03)

Virtual Tree .NET Control Advanced data binding Fast virtual loading of data [Link]

Visual [Link] Course Learn comfortably and quickly from your home. Get informed here! [Link]

Design and animation courses ["editorial","web","modeling","animation photoshop","maya","3dstudio","premiere"]


[Link]

Introduction
Yes, I know that if we are going to create a client in Visual Basic 6.0, it should not be in the .NET section.
but... what it's about is using a Web Service created with a .NET language
Framework (particularly with C#), as I believe that 'also' should be in this section...

De lo que setrata es de saber qué es lo que tenemos que hacer para podercrear uncliente (una
classic Visual Basic application to be able to use a Web service created with .NET.
The good thing about using Web services is that by accessing them via the internet, you do not have to
we should worry if it's made in .NET or not... the important thing is that we can use it and that's what matters here
let's see:

How to use an XML Web service from VB6


[Link]/…/[Link] 1/11
17/01/2011 How... How to use a Web XM service...
(or how to create a VB6 client to use an XML Web service)
Let's start with what we need to be able to create the project (or use the example code included
in the zip file).

The first thing is Visual Basic 6.0 (it is supposed to work with VB5, but I haven't checked it)
2- The project will use the Microsoft Internet Transfer Control (INET) to access the
site where the Web service is hosted (even if it is on the same machine)
3- A reference to Microsoft XML version 2 (any later version is also acceptable) will also be used.
4- If you are going to have the Web services on your own machine, you will need Windows XP Professional (or the
Windows 2000) with IIS installed (to be able to use it from, for example, localhost), in addition to
having the .NET Framework SDK installed, as the .NET web services require the infrastructure
from .NET (elemental), but this last one only if you are not going to use the Web service that is published in
Costasol pages.

Once the issue of the requirements is clarified, let's go to the explanation of how to create the client in VB6.
to access a Web service, in particular it will be the Web service that converts degrees
Celsius to Fahrenheit ([Link], whose code you can see by following this link).

Note:
If you want to try the Web service from the internet, you will have to use this address:
[Link]
from her you can try it.

Part of the code used to send to the Web service is obtained from the description shown in the
explorer, for example, the way to call each of the functions. Therefore, this example
que te voy a mostrar sólo es válido para este servicio Web, en otra ocasión veremos cómocrear
a generic client for any web service, in this example we will see how to know what functions
exporta el servicio Web y los parámetros que necesita recibir, para ello necesitaremos examinar y
understand the content of a file of the type WSDL (Web Service Description Language, language of
Web service description), but that will be on another occasion.

When we send the information to the web service, we have to do it using the protocols that the
.NET understands, the one used in this example is SOAP, I am not going to go into details, just to tell you
that the format used for communication between a Web service and the client will always be using
XML (hence the name of these Web services), therefore the SOAP code that I am going to show you (the
which is taken, as I mentioned before of the page shown by the explorer) is in that
format, that is the reason we have a reference to Microsoft XML version 2, since we will use
some of the classes included in it to be able to handle the XML code, well, okay, I won't go on
Here you have the code for one of the functions, in particular the function that converts degrees.
Celsius to Fahrenheit:

The provided text appears to be a fragment of an XML or SOAP message and does not contain translatable content.
<soap:Body>
<CaF xmlns="[Link]
<valor> double </valor>
</CaF>
</soap:Body>
</soap:Envelope>

When we send this code to the Web service, we will have to include it in the site that is marked.
in bold (the word double) the value of the parameter that we want to pass to that function.
We will do this using the DOMDocument class from the msxml library. The code would be something like this:

[Link]/…/[Link] 2/11
17/01/2011 How... How to use a Web XM service...

Private Const cSOAPCaF = _


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="[Link] xmln
<soap:Body>
<CaF xmlns="[Link]
<value>1</value>
</CaF>
</soap:Body>
</soap:Envelope>

Dim parser As DOMDocument


Set parser = New DOMDocument
load the SOAP code for CaF
[Link] cSOAPCaF
'
Indicate the parameter to send
[Link]("/soap:Envelope/soap:Body/CaF/valor").Text = [Link]

With this last line, what we are doing is assigning the parameter that we want to pass to the function CaF,
which expects a double type value in a parameter called value.

Once we have the XML code with the data to send to the Web service, we will use the Internet
Transfer to make that shipment, the following line is responsible for that:

Use the Inet control to make the call to the Web service
[Link] [Link], "POST", [Link], "Content-Type: text/xml; charset=u"

Once this command has been executed, the Web service will return the response to us, (even if...
An error occurs.
To receive (and interpret) the response that the Web service sends us, we will continue using the control.
Inet, in particular the eventStateChanged, let's take a look at the code:

[Link]/…/[Link] 3/11
17/01/2011 How... How to use a Web Service XM...

Private Sub Inet1_StateChanged(ByVal StateAs Integer


'
If (State = icResponseCompleted) Then ' icResponseCompleted = 12
Let sAs String
'
Read the data returned by the server
s = [Link](4096)
'
Put the data in the XML parser
Set parser as DOMDocument
Set parser = New DOMDocument
[Link] s
'
On Error Resume Next

[Link] = [Link]("/soap:Envelope/soap:Body/CaFResponse")

End If
End Sub

What is done at this event?


It is checked whether the control state is correct (the description of the state icResponseCompleted is:
The request was completed and all the data was received), that is to say, if State is 12 it means
that we have the server's response, so we read that data, we pass it to the XML object
so that it is easier for us to interpret them and we display the received result in a text box.

Note:
It is important to detect the errors, since if the string sent to the Web service does not
It is correct, an error will occur when reading the value, especially if the received string is not
in XML format, what will happen if, for example, if the indicated address is not
web service, in this case it will return a "normal" HTML page indicating that it does not
has found (the typical error 440).

And so, broadly speaking, this is what we need to create the VB6 client, not much, right?
The problem is knowing which code to use for calling the different functions, luckily,
if we access a Web service from the explorer, it will show us a page with what is necessary
to create the code we need. In the following figure, you will see who I am referring to:

[Link]/…/[Link] 4/11
17/01/2011 How... How to use a Web XM service...

Figure 1, the SOAP code to call the function of the Web service

Yes, I know that's not very clear, but... once you know how to 'understand it',
the thing turns out easier... I, the truth is that when I saw that code, well... like nothing... but,
Fortunately, I came across a book in which, although it does not thoroughly explain how to do this, in the
Web of the same yes, it included an example, which has helped me to create this one that I show you.
Today... don't think that I know everything... I also read other authors..., in this case it is
David S. Platty in his book Introducing .NET (which was sent to me by the people from MSDN Latin America)
achieve the first 5-star developer star, which must be said.

Well, I’ll leave you the complete code, as well as a screenshot of the form in addition to, of course,
I will leave the example code in a zip file.

The capture of the sample form:

[Link]/…/[Link] 5/11
17/01/2011 How... How to use a Web XM service...

Figure 2, Capture of the form at runtime

The link to the complete code (the Web service file and the WSDL file are included) [Link]
10.20 KB

See you.
Guillermo
P.S.
Remember that soon I will publish the generic code to access any Web service.
In that example, I will show the way to create a series of collections with the different functions that I
they include in the Web service as well as each of the parameters received each of the
functions.
But that will be in a few days... or perhaps tomorrow... who knows?

Code for Visual Basic 6


Este es el código del formulario:

'------------------------------------------------------------------------------
VB6 client of the Web service converter from °C to °F (Jun 18, 03)
'
Based on an example from the book: Introducing .NET by David S. Platt
'
©Guillermo 'guille' Som, 2003
'------------------------------------------------------------------------------
Option Explicit

Private Enum tipoConversion


deCaF
deFaC
End Enum
Private tipoAs tipoConversion

These definitions are taken from what is shown in the explorer.


when selecting each of the functions of the Web service
Private Const cSOAPCaF = _
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="[Link] xmln
<soap:Body>
<CaF xmlns="[Link]
<value>1</value>
"</CaF>" & _
[Link]/…/[Link] 6/11
17/01/2011 How... How to use a Web XM service...
</CaF>
</soap:Body>
"</soap:Envelope>"

Private Const cSOAPFaC = _


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="[Link] xmln
<soap:Body>
<FaC xmlns="[Link]
"<valor>1</valor>" & _
</FaC>
</soap:Body>
</soap:Envelope>

Private Sub cmdC2F_Click()


tipo = deCaF
Dim parser As DOMDocument
Set parser = New DOMDocument
load the SOAP code for CaF
[Link] cSOAPCaF
'
Indicate the parameter to be sent
[Link]("/soap:Envelope/soap:Body/CaF/valor").Text = [Link]
'
Show the XML code sent to the Web service
[Link] = [Link]
Use the Inet control to perform the HTTP POST operation
[Link] [Link], "POST", [Link], "Content-Type: text/xml; chars
End Under

Private Sub cmdF2C_Click()


tipo = deFaC
Dim parser As DOMDocument
Set parser = New DOMDocument
load the SOAP code for FaC
[Link] cSOAPFaC
'
Indicate the parameter to be sent
[Link]("/soap:Envelope/soap:Body/FaC/valor").Text = [Link]
'
Show the XML code sent to the Web service
[Link] = [Link]
Use the Inet control to perform the HTTP POST operation
[Link] [Link], "POST", [Link], "Content-Type: text/xml; chars
End Sub

Private Sub Form_Load()


[Link] = "Cliente VB6" & vbCrLf & "del Servicio Web Conversor de ºC a ºF"
End Sub

Private Sub Inet1_StateChanged(ByVal StateAs Integer


'
If (State = icResponseCompleted) Then ' icResponseCompleted = 12
Dim sAs String
'
Read the data returned by the server
s = [Link](4096)
[Link] = s
'
Put the data in the XML parser
Dim parser As DOMDocument
Set parser = New DOMDocument
[Link] s
'
On Error Resume Next
[Link]/…/[Link] 7/11
17/01/2011 How... How to use a Web Service XM...
'
If type = deCaFThen
[Link] = [Link]("/soap:Envelope/soap:Body/CaFResp")
Else
[Link] = [Link]("/soap:Envelope/soap:Body/FaCResp")
End If
'
If [Link] > 0 Then
[Link]
End If
End If
End Sub

The web service code created with C#

This is the code used to create the Web service.


It will be inside a file called [Link] which will need to be copied into a folder
from the local Web server (if your intention is to test it without being connected to the Internet).

<%@ WebService Language="c#" Class="[Link]" %>

using [Link];

namespace CelsiusFahrenheit
{
///
Brief description of Converter.
///
[WebService(Namespace="http:"//elGuille/WebServices",
Description="Conversor de grados Fahrenheit a grados centígrados
public class Converter
{
//
const double fc = (5.0 / 9.0);
//
Converts from Fahrenheit to Celsius, returns a value
public double FaC(double value)
{
return ((value - 32) * fc);
}
[WebMethod(Description="Convierte de Celsius a Fahrenheit, devuelve un va
public double CaF(double value)
{
return (value / fc + 32);
}
//
}
}

Modification to NOT use the Internet Transfer Control to access the Web service
(29/Jun/03)

In the example I showed above, the INET control (Internet Transfer Control) is used to access.
to the web service, both to send the data and to receive it, but if you don't want to, it's not necessary anymore.

[Link]/…/[Link] 8/11
17/01/2011 How... How to use a Web XM service...
that Microsoft XML version 2.0 includes an object to perform this task, it is about
XMLHTTPRequest.
This object contains a series of methods that allow us to send a command to the Web service to
to send the information and it will also allow us to receive the data that the Web service sends us.

We will see the code that needs to be changed for the project to access the service.
Web temperature converter.

The first thing we need is the controlInet, so you can remove it from the form, as well as
the reference we have (in Project/Components...)
You can also remove the Intet1_Statechanged event as we will not need it.

Here (within a few lines) I will show you the new code. The first thing you will have to do is
replace the click event code of the two conversion buttons, in addition to adding a couple of
new procedures for sending and receiving information from the Web service.
Although I'm going to explain a little bit how the XMLHTTPRequest object works and how to use it.

When we send a POST command (we will see how shortly), the command is sent to the Web service.
(just like we did with the controlInet) and the Web service will return a response to us, that
the response will also have to be processed by the objectXMLHTTPRequest. How can it be that the
response is slow to arrive, could we be notified when that response arrives (way
asynchronous) or we can also wait until the response arrives and then continue (form
synchronous). This last one will be the form we are going to use, since the other one (the asynchronous) needs something
other code and even the creation of a class, which would be the one to receive the notification that it
It has finished receiving data from the server.

Note:
If you want to see how this is done, I recommend that you download Microsoft XML.
4.0 Parser SDK in the documentation is how to do it, (this other link will take you to the
MSXML 4.0 SDK documentation online and in English.
If you have the documentation of the mentioned SDK, you can search for these topics:
Use the onReadyStateChange Property (Visual Basic)
Make XML Requests Over HTTP (Visual Basic)
Program with DOM in Visual Basic

Well, now let's take a look at the code.


If you want to download the already 'made' code, click this link. [Link] 8.91 KB

The code for the two Click events (you will need to replace this code with the code shown)
previously

[Link]/…/[Link] 9/11
17/01/2011 How... How to use a Web XM service...

Private Sub cmdC2F_Click()


type = deCaF
'
Dim parser As DOMDocument
Set parser = New DOMDocument
load the SOAP code for CaF
[Link] cSOAPCaF
'
Indicate the parameter to be sent
[Link]("/soap:Envelope/soap:Body/CaF/valor").Text = [Link]
'
Show the XML code sent to the Web service
[Link] = [Link]
'
'
sendCommand [Link], "[Link]
'
End Sub

Private Sub cmdF2C_Click()


tipo = deFaC
'
Dim parser As DOMDocument
Set parser = New DOMDocument
load the SOAP code for FaC
[Link] cSOAPFaC
'
Indicate the parameter to be sent
[Link]("/soap:Envelope/soap:Body/FaC/valor").Text = [Link]
'
Show the XML code sent to the Web service
[Link] = [Link]
'
'
enviarComando [Link], "http:"//elGuille/WebServices/FaC"
'
End Sub

The new code to send the command to the Web service and the one that processes the received code from the
web service.

[Link]/…/[Link] 10/11
January 17, 2011 How... How to use a Web Service XM...

Private Sub sendCommand(ByVal sXmlAs String, ByVal sSoapAction As String


Send the command to the Web service
'
use XMLHTTPRequest to send the information to the Web service
Set oHttReq As XMLHTTPRequest
Set oHttReq = New XMLHTTPRequest
'
Send the command synchronously (it waits for the response to be received)
[Link] "POST", [Link], False
the headers to be sent to the Web service
(do not include the colon in the header name)
[Link] "Content-Type", "text/xml; charset=utf-8"
[Link] "SOAPAction", sSoapAction
send the command
[Link] sXml
'
this will be the text received from the Web service
processResponse [Link]
'
End Sub

Private Sub processResponse(ByVal sAs String)


process the response received from the Web service
[Link] = s
'
Put the data in the XML parser
Dim parser As DOMDocument
Set parser = New DOMDocument
[Link] s
'
On Error Resume Next
'
If type = deCaFThen
[Link] = "Error"
[Link] = [Link]("/soap:Envelope/soap:Body/CaFResponse
Else
[Link] = "Error"
[Link] = [Link]("/soap:Envelope/soap:Body/FaCResponse"
End If
'
If [Link] > 0 Then
[Link]
End If
End Sub

If you observe the changes, you will realize that using the XMLHttpRequest object we have to
The headers sent to the Web service are indicated using the setRequestHeader method.
XML code is sent with the send method.

Well... I hope you now know another way to communicate with a Web XML service created with
.NET

See you.
William

[Link]/…/[Link] 11/11

You might also like