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; }
    }
}

posted on 2009-04-13 16:36  vcool  阅读(3365)  评论(0编辑  收藏  举报