VB6与WebService中的自定义类

前端之家收集整理的这篇文章主要介绍了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 原文链接:https://www.f2er.com/vb/261115.html

猜你在找的VB相关文章