微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb调用webservice(二)

利用SOAP协议

首先,需要引用 microsoft xml 6.0

在客户端调用

'定义soap消息 这个消息可以在webservice调用过程获得。主要处理在soap12:Body
Dim strtest As String


' strtest = "<?xml version=""1.0"" encoding=""utf-8""?> "
' strtest = strtest + " <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> "
' strtest = strtest + " <soap12:Body> "
' strtest = strtest + " <HelloWorld xmlns=""http://tempuri.org/""> "
' strtest = strtest + " <str>" + str + "</str> "
' strtest = strtest + " </HelloWorld> "
' strtest = strtest + " </soap12:Body> "
' strtest = strtest + " </soap12:Envelope> "
strtest = "<?xml version=""1.0"" encoding=""utf-8""?> "
strtest = strtest + " <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> "
strtest = strtest + " <soap12:Body> "
strtest = strtest + " <returnclasstest xmlns=""http://tempuri.org/"" /> "
strtest = strtest + " </soap12:Body> "
strtest = strtest + " </soap12:Envelope> "
strxml = strtest

'定义一个http对象,一边向服务器发送post消息
Dim h As MSXML2.ServerXMLHTTP40
'定义一个XML的文档对象,将手写的或者接受的XML内容转换成XML对象
Dim x As MSXML2.DOMDocument40
'初始化XML对象
Set x = New MSXML2.DOMDocument40
'将手写的SOAP字符串转换为XML对象
x.loadXML strxml
'初始化http对象
Set h = New MSXML2.ServerXMLHTTP40
'向指定的URL发送Post消息。这里的webservice地址为需要引用的地址
h.open "POST","http://localhost/webserice/Service.asmx",False
h.setRequestHeader "Content-Type","text/xml"
h.send (strxml)
While h.readyState <> 4
Wend
'显示返回的XML信息
Text1.Text = h.responseText

至此,获得webservice返回消息成功 webservice返回值可以为自定义
在VB6.0 vs2005下调试成功

下附webservice源码:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod] public string HelloWorld(string str) { return "Hello World :" + str; } [WebMethod] public int add(int i,int j) { return i + j; } [WebMethod] public returnclass returnclasstest() { returnclass r = new returnclass(); r.ID = "IDtest"; r.NAME ="NAMEtest"; return r; }}public class returnclass{ public returnclass() { } private string id; private string name; public string ID { get { return id; } set { id = value; } } public string NAME { get { return name; } set { name = value; } }}

原文地址:https://www.jb51.cc/vb/261887.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐