vc6如何调用WEBSERVICE

来源:互联网 发布:mac下客户端看视频 编辑:程序博客网 时间:2024/06/06 08:26

1.创建一个VC项目,创建一个类代码如下

-----------WSWrapper.h----------------------------------------------------------------------------

// WSWrapper.h: interface for the WSWrapper class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_WSWRAPPER_H__202DF96C_D7E7_4D5C_BE38_FA077AFAD139__INCLUDED_)
#define AFX_WSWRAPPER_H__202DF96C_D7E7_4D5C_BE38_FA077AFAD139__INCLUDED_
#import "msxml4.dll"
#import "mssoap30.dll" \
            exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
                    "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
#include <string>
#include <Windows.h>

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
using namespace MSXML2;
using namespace MSSOAPLib30;  
using std::string;



class WSWrapper  
{
public:
    WSWrapper();
    WSWrapper(const char *wsURL, const char *wsNameSapce, const char *wsMethodName);
    virtual ~WSWrapper();    
    string Hello(const string &strName);
private:
     const string _wsURL;
     const string _wsNameSapce;
     const string _wsMethodName;
};

#endif // !defined(AFX_WSWRAPPER_H__202DF96C_D7E7_4D5C_BE38_FA077AFAD139__INCLUDED_)


-------------WSWrapper.cpp-----------------------------------------------------------------------------------------------------------------------

// WSWrapper.cpp: implementation of the WSWrapper class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CALLWEBSERVICE.h"
#include "WSWrapper.h"
#include "WSWrapper.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

WSWrapper::WSWrapper()
{
   
}
WSWrapper::WSWrapper(const char *wsURL,  const char *wsNameSapce, const char *wsMethodName) : _wsURL(wsURL), _wsNameSapce(wsNameSapce), _wsMethodName(wsMethodName)
{
    
}


WSWrapper::~WSWrapper()
{

}
string WSWrapper::Hello(const string &strName)
{    
    try
    {
        HRESULT hr = CoInitialize(NULL);//初始化com环境
        if(FAILED(hr))
        {
            //出错了
        }

       ISoapSerializerPtr Serializer;
       ISoapReaderPtr Reader;
       ISoapConnectorPtr Connector;

        //连接到WebService
        hr = Connector.CreateInstance(__uuidof(HttpConnector30));
        if(FAILED(hr))
        {
            return "create webservice err";//创建com对象出错,一般是因为没有安装com
        }

        Connector->Property["EndPointURL"] = _wsURL.c_str();
        Connector->Connect();
        Connector->Property["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();        

       //开始创建webservice的请求Soap包
        Connector->BeginMessage();
        hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
        if(FAILED(hr))
        {
           return "return soap err";//创建com对象出错,一般是因为没有安装com
        }
        Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
        Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");
        Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
        Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");

        Serializer->StartBody(L"NONE");
        Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
        Serializer->StartElement(L"strName", "", "NONE", "");
        Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
        Serializer->WriteString(strName.c_str());        
        Serializer->EndElement();
        Serializer->EndElement();
        Serializer->EndBody();
        Serializer->EndEnvelope();        

        Connector->EndMessage();

        //解析返回的soap包
        hr = Reader.CreateInstance(__uuidof(SoapReader30));
        if(FAILED(hr))
        {
            
            return "CAREE 解析返回的soap包 ERR";//创建com对象出错,一般是因为没有安装com
        }
        Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
        string strResult((const char*)Reader->RpcResult->text);
                
        return strResult;
        
        
    }
    catch( _com_error e)
    {            
        //return (CString)(char*)e.Description;
    }
    return "error";
}
-----------------在窗口上增加一个按钮事件---------------------------------------------------------------
void CCALLWEBSERVICEDlg::OnButton1()
{
     WSWrapper wsWrapper("http://localhost/TestWebService/Service1.asmx?wsdl",
        "http://localhost/TestWebService/",
        "Hello");

    string strName = "Lucy";
    string strResult = wsWrapper.Hello(strName);
    CString returns;
    returns.Format("%s", strResult.c_str());

    m_return.SetWindowText(returns);
    //SetTimer(3,1000,NULL);
}
--------------------用c#写一个webservice-----项目名称为TestWebService------------

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace TestWebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://localhost/TestWebService/")]
    [System.Web.Services.Protocols.SoapRpcService]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        //[WebMethod(MessageName = "HelloWithName")]
        [WebMethod]
        public string Hello(string strName)
        {
            return "Hello, " + strName + " call webserice OK!";
        }
    }
}

---------------------------------------------------------------------------------------


原创粉丝点击