VB6与WebService中的自定义类

两种情况:

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.Extensions
public 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,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","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 If

End Sub
以上代码 VS2005 C# + VB6

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...