vb.net 接收xml_发送和接收XML或HTML数据,使用VB6与Web服务进行通信

vb.net 接收xml

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web services on my own network, so I thought I’d share this with a few examples as starting points.  This is not intended to be a fully working model for communicating with any given web service, but simply a foundation for you to do your own testing within VB6 to communicate with servers that provide web service functionality.  There are three starting examples included based on questions I found here on EE:

我见过很多人在寻找有关如何从VB6访问Web服务的示例。 我一直在使用我在VB6中内置的测试工具(利用我在网上找到的许多资源)将其用于小型项目,以确定如何与自己的网络上的Web服务进行通信,因此我想与几个例子作为起点。 这并不是要成为与任何给定Web服务进行通信的完全正常工作的模型,而仅仅是为您在VB6中进行自己的测试以与提供Web服务功能的服务器进行通信的基础。 根据我在EE上发现的问题,包含了三个开始的示例:

-      Dial a Cisco phone (This is the only example that fully works.)

-拨打Cisco电话(这是唯一可以正常使用的示例。)

-      Twitter oAuth token request

-Twitter oAuth令牌请求

-      eBay service comm

-eBay服务通讯

The core of this test harness is the “Microsoft WinHTTP Services” Reference.  The main form (frmPostGet), shown below, has the key components in the UI for communicating with a web service:

该测试工具的核心是“ Microsoft WinHTTP Services”参考。 如下所示,主要形式(frmPostGet)具有UI中用于与Web服务通信的关键组件:

-      URL

-网址

-      Headers

-标头

-      Data

-数据

-      …and Results returned from the web service.

-…结果从Web服务返回。

Screenshot of successful dialing of Cisco phone

The way to use this test harness is to populate the URL, Headers, and Data with service-specific strings, then Send the data and review the results in the “Results” area.  Once you have a working process, you can begin to code your own client.  The working examples combo box will help fill in some of this information for the included examples.

使用此测试工具的方法是使用服务特定的字符串填充URL,标头和数据,然后发送数据并在“结果”区域中查看结果。 一旦有了工作流程,就可以开始编写自己的客户端了。 工作示例组合框将有助于为所包括的示例填写一些信息。

The [Send] button is where the action takes place.  See the attached project for the specific code.  It has this sequence of steps:

[Send]按钮是执行操作的地方。 有关特定代码,请参见附件项目。 它具有以下步骤序列:

Initialize the WinHTTPRequest variable:

初始化WinHTTPRequest变量:

Set HttpReq = New WinHttpRequest

Open a connection to the web service:

打开与Web服务的连接:

HttpReq.Open Type_of_Request, URL_of_WebService, Handle_Asynchronously

Add headers one row at a time.  Each row is separated by CR-LF, then each header name-value pair is separated by a colon.  I used an array and the Split() function to easily handle these separations.

一次添加标题一次。 每行用CR-LF分隔,然后每个标题名称/值对用冒号分隔。 我使用了一个数组和Split()函数来轻松处理这些分离。

arrHeaders = Split(Headers_String, vbCrLf)
For i = 0 To UBound(arrHeaders)
     HttpReq.SetRequestHeader Split(arrHeaders(i), ":")(0), Split(arrHeaders(i), ":")(1)
Next i

Send the data:

发送数据:

On Error GoTo Error_HTTPSend
HttpReq.Send Data_to_Send     'Note - in many cases, if not using POST or PUT, may need to send only ""
On Error GoTo 0

Finally, I format results into presentable format with CR-LF and push that into the Results field on the form:

最后,我使用CR-LF将结果格式化为可显示的格式,并将其推入表单的“结果”字段中:

txtResults.Text = Replace(HttpReq.ResponseText, vbLf, vbCrLf)

Depending on the web service, you will look in the Results field to find what defines success or failure.  In the example shown in the screenshot above, the Cisco phone responded with XML data with one of the elements showing the text “Success” (plus I saw the phone actually dial!)

根据Web服务的不同,您将在“结果”字段中查找定义成功或失败的内容。 在上面的屏幕快照中显示的示例中,Cisco电话以XML数据作为响应,其中的元素之一显示了文本“成功”(此外,我看到电话实际上正在拨号!)

There are two modules in addition to the main form (basBase64Utils and basURLHelper) that are not essential to the WinHTTP services, but are used to simply aid in the use of the test harness.  

除了主要形式(basBase64Utils和basURLHelper)以外,还有两个模块对WinHTTP服务不是必需的,但仅用于辅助测试工具的使用。

basBase64Utils  - Web services usually require the authentication user/password to be encoded in Base64 format, so these routines do this coding/decoding.  There is also a routine to get a complete file as a Base64 string, for instance to upload to a web service or include as an email attachment.  Most of this module is from the author at http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp.

basBase64Utils-Web服务通常要求将身份验证用户/密码以Base64格式进行编码,因此这些例程将执行此编码/解码。 还有一个例程可以获取完整的文件作为Base64字符串,例如,将其上传到Web服务或作为电子邮件附件包含在内。 此模块的大部分内容来自作者, 网址http://www.nonhostile.com/ 如何编码 解码 base64-vb6 .asp

basURLHelper – This module is used to easily build and parse the URL field on the form.  These routines do the work of determining the protocol, the website URL, and the URI.  These are from the authors at http://www.vbforums.com/showthread.php?t=330698 and http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22503490.html.

basURLHelper –此模块用于轻松构建和解析表单上的URL字段。 这些例程负责确定协议,网站URL和URI。 这些来自http://www.vbforums.com/sh上的作者 owthread.p hp?t = 33069 8http://www.experts-exchang e.com/Prog 夯/拉 语言/ Vi sual_Basic / Q_2250349 0.html

This test harness works with XML and HTML data, since it is using the generic WinHTTP Services to communicate with the web services server.  It is up to you to parse the results depending on your specific needs, but there are many tools available online to make this easier.

该测试工具使用XML和HTML数据,因为它使用通用的WinHTTP Services与Web服务服务器进行通信。 您可以根据自己的特定需求来解析结果,但是可以使用许多在线工具来简化此过程。

I hope this helps others as it has me.

我希望这对其他人有帮助。

TestWebService-r1.zip TestWebService-r1.zip

翻译自: https://www.experts-exchange.com/articles/6556/Sending-and-receiving-XML-or-HTML-data-communicating-with-web-services-using-VB6.html

vb.net 接收xml