使用 VB6.0 应用程序消费 .NET Web 服务






4.96/5 (14投票s)
我将向您展示如何从使用 .NET 开发的 Web 服务(版本无关紧要)获取响应。
引言
Web 服务在信息管理中扮演着重要的角色。
因此,尽管信息技术在不断进步,但我们也不能忘记过去。
使用与开发 Web 服务相同的技术来消费它……很容易。但是,如果我们想使用像旧的 Visual Basic 6.0 这样的另一种技术来消费该 Web 服务呢?仍然需要这样做吗?当然。这就是为什么我呈现这篇关于我们如何使用 Visual Basic 6.0 开发的应用程序来消费 .NET 开发的 Web 服务的文章。
我们开始吧。祝大家幸运!
背景
您可能对 XML 有一些了解,并且可选地安装了 SOAP TOOLKIT 3.0 或更高版本(以防您在使用 MSXML2.DOMDocument
时遇到问题),您可以在 Microsoft 官方网站或 Google 上找到它:SOAP TOOLKIT 3.0 DOWNLOAD :D,但我在想……如果 MSXML2 库随 Internet Explorer 8 一起安装呢?
对于 Web 服务:VS 2008 或 VS 2010。
Using the Code
我们将开始。
在这个 VB 6.0 草稿中,我们有 4 个窗体和一个名为 XMLRequestNuic
的类。XMLRequestNuic
类负责向 Web 服务发送 XML 主体请求。请记住,有许多方法可以查询 Web 服务,但这种方式对于开发人员来说更加透明和易于理解。
第一个窗体称为 frmMain
,它存储用户界面,其他窗体仅存储图片作为帮助。
我将详细解释如何构建 XML 请求以及如何简单地获取响应。
首先,我们看简单部分,然后提高难度。
考虑以下代码片段
''
'' 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
...
下面的代码片段初始化了 XMLrequestNuic
类。这个类将在下面详细介绍,不用担心。
然后,声明一个名为 <aDatos()>
的数组变量,它将存储查询值??
我们还有一个名为 <iTotalElem>
的变量,它将用于存储提交给 Web 服务的条目数。
我们还有一个布尔变量,用于验证 XML 结构是否正确。
然后我们将使用变量 iCant
来构建循环类型的变量 <@ parameter1>
、<@ parameter2>
等等。
''
''
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
在下面的代码片段中,我们对字段 txtCriterios.Text
应用了 SPLIT 函数 ()
,该字段是我们键入要发送到 Web 服务的 txtCriterios.Text
值的输入框。
为了执行 split ()
,值由以下任何分隔符分隔:“,” 或 “-” 或 “.” 或 “+”,当然,如果您想添加更多分隔符,也可以。继续解释,当我们执行 split ()
后,然后将结果存储在变量 <aDatos()>
中,然后将元素数量赋给变量:<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: 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.
下一步是验证 XML 结构是否包含最少的标签。
''
'' 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
下面的代码片段开始创建输入参数,但是我们必须为每个查询值创建正确的格式,即,我们需要创建如下变量:<@ parametro1<code><code>>
、@ parametro2、@parametro3 @>。这取决于 Web 服务需要返回结果的数据量。
为此,应用程序需要 chkString.Value = 1
。查找单词“string
”,如果您忽略此复选框,那么我们将不得不手动为 XML 的每个“String
”单词写 <@ Parametro (NumberOfInputValue)>
。
在这种情况下,我们这样做。
我们创建一个循环,该循环搜索单词“String
”并将其替换为“@ parametro1
”,如果找到第二个单词“string
”,则将其替换为“@ Parametro2
”,依此类推。最终结果是 XML 的根……但其中“string
”字样已被找到的第一个“string
”字样替换为 @parameter1
,第二个“string
”字样被替换为 @parameter2
。
请记住:您可以添加更多数据类型,如 INTEGER
、BOOLEAN
、Float
等,但请记住,如果 Web 服务未传递非 STRING
类型的数据类型的值……它将引发错误。这就是为什么我只提供 STRING
类型示例的原因。
''
'' 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
下面的代码片段现在将“@parametro1
”替换为您在 txtCriterios.text
字段中键入的第一个值,依此类推。
搜索 @Parametro
& iCant
,如果找到,则继续执行替换函数。
'''
''
'''
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
现在,让我们看看最后一步……这个项目的核心:XMLrequestNuic
类。这个类是我们的 XML 主体成形并从 Web 服务获取数据的地方。
基本上,该类通过 Msxml2.XMLHTTP
对象发出请求,该对象负责返回包含所有字段和值的 XML,我们只需要读取该结果并获取我们需要的字段的值。
''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
关注点
正如您所见,这篇文章很简单。我假设许多开发人员仍然对如何处理 XML 结构知之甚少,因此在第二部分中,我将更新这篇文章,讨论如何处理 XML 节点。
如果这篇文章有帮助,请投票。