VB6与WebService中的自定义类
来源:互联网 发布:手机淘宝客转链工具 编辑:程序博客网 时间:2024/06/07 20:46
两种情况:
1. WebService中返回自定义类
2. WebService中的参数是自定义类
简单示例:
WebService代码:
using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Serialization; [WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService] //这个要右键“添加引用”:System.Web.Extensionspublic class Service : System.Web.Services.WebService{ public Service () {} [WebMethod] public string HelloWorld() { return "Hello World"; } //这个服务返回一个自定义类 [XmlInclude(typeof(Student))] //用于返回自定义类的序列化 [WebMethod] public Student SetStudent(string pID, string pName, int pAge) { Student myStudent = new Student(pID,pName,pAge); return myStudent; } //这个服务传入一个自定义类参数,返回一个string [WebMethod] public string GetStudentID(Student pStudent) { return pStudent.sid; }}//自定义通用类Student:using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;/// <summary>/// Student 的摘要说明/// </summary>[Serializable]public class Student{ private string _sid; private string _sname; private int _age ; public string sid { get{ return _sid;} set{ _sid = value; } } public string sname { get { return _sname; } set { _sname = value; } } public int age { get { return _age; } set { _age = value; } } public Student() { } public Student(string pID,string pName,int pAge) { _sid = pID; _sname = pName; _age = pAge; }}
VB6代码(引用Microsoft XML , v6.0):
1. WebService中返回自定义类
Private Sub Command8_Click() '返回WS中的自定义类型 Dim oHTTP As XMLHTTP Dim oXmlDoc As DOMDocument Dim strWebserviceURL As String, strRequest As String 'HTTP POST strWebserviceURL = "http://localhost:1510/MyWebTest/Service.asmx/SetStudent" strRequest = "pID=003&pName=admin&pAge=22" '三个参数用&号分隔 Set oHTTP = New XMLHTTP oHTTP.open "POST", strWebserviceURL oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" oHTTP.send strRequest While oHTTP.readyState <> 4 DoEvents Wend Set oXmlDoc = New DOMDocument oXmlDoc.Load oHTTP.responseXML If oXmlDoc.parseError Then MsgBox oXmlDoc.parseError.reason Else MsgBox oXmlDoc.Text End If End Sub
2. WebService中的参数是自定义类。这个要使用基元类型作为参数的方法:
Private Sub Command16_Click() Dim oHTTP As XMLHTTP Dim oXmlDoc As DOMDocument Dim strWebserviceURL As String, strEnvelope As String 'Web服务 strWebserviceURL = "http://localhost:1510/MyWebTest/Service.asmx" '构造POST信息(这个运行上面的WebService,选择GetStudentID,在SOAP 1.2 请求响应示例中有相关内容) strEnvelope = "<?xml version=""1.0"" encoding=""utf-8""?>" strEnvelope = strEnvelope & "<soap:Envelope " & _ "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _ "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & _ "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" strEnvelope = strEnvelope & "<soap:Body>" strEnvelope = strEnvelope & "<GetStudentID xmlns=""http://tempuri.org/"">" strEnvelope = strEnvelope & "<pStudent>" strEnvelope = strEnvelope & "<sid>009</sid>" '设置sid=009 strEnvelope = strEnvelope & "<sname>ABCD</sname>" '设置sname=ABCD strEnvelope = strEnvelope & "<age>20</age>" '设置age=20 strEnvelope = strEnvelope & "</pStudent>" strEnvelope = strEnvelope & "</GetStudentID>" strEnvelope = strEnvelope & "</soap:Body>" strEnvelope = strEnvelope & "</soap:Envelope>" 'POST Set oHTTP = New XMLHTTP oHTTP.open "POST", strWebserviceURL oHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8" oHTTP.setRequestHeader "SOAPAction", "http://tempuri.org/GetStudentID" oHTTP.send strEnvelope While oHTTP.readyState <> 4 DoEvents Wend '获取返回值 Set oXmlDoc = New DOMDocument oXmlDoc.Load oHTTP.responseXML If oXmlDoc.parseError Then Debug.Print oXmlDoc.parseError.reason Else MsgBox oXmlDoc.Text End IfEnd Sub
以上代码 VS2005 C# + VB6
- VB6与WebService中的自定义类
- 浅谈VB6中的自定义类的使用
- 浅谈VB6中的自定义类的使用
- 浅谈VB6中的自定义类的使用
- 浅谈VB6中的自定义类的使用
- 【VB6】VB6类库项目中的MTSTransactionMode
- 【VB6】VB6类库项目中的Persistable
- flex调用webservice中的自定义类
- VB6调用WebService、WCF
- webservice 传递自定义类
- VB6.0调用WEBService问题
- VB6.0调用WEBService问题
- vb6中的hook技术
- EXCEL表格中直接使用vb6创建的dll中的自定义函数
- VB6 DataReport打印自定义纸张
- 写给痛苦于新旧语言交互中的新人们,vb6调用vs2005的webservice数据(方法一)
- VB6 与 .NET 联合
- VB6基础教程与源代码
- 'AjaxPro'未定义错误的原因&javascript顺序执行&AjaxPro机制
- 软件滤波
- javascript自执行匿名函数
- copy_to_user 和 copy_from_user
- 谷歌Android UI设计技巧:优秀UI设计准则
- VB6与WebService中的自定义类
- android sp wp
- windows 7 下出现 Microsoft .NET Framework 相关未能初始化的信息
- DM365 OSD Memory DDR 映射 及寄存器说明
- GCC 命令行详解
- equals方法和toString方法
- Linux内核抢占实现机制分析
- Javascript版的全文过滤特殊字母
- ORA-22913: must specify table name for nested table column or attribute