1.首先用C#创设WebService。源码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace Soap
{
    [WebService(Namespace = “http://Fantasy/WebServiceDemo/")\]
    [System.Web.Services.Protocols.SoapRpcService]
    public class Service : System.Web.Services.WebService
    {
        public Service()
        {

        }

        [WebMethod]

        public int AddNumbers(int NumberOne, int NumberTwo)
        {

            return NumberOne + NumberTwo;

        }
       
    }
}

2.因为黑白托管的vc,所以需求选拔微软的soapsdk3.0
 1.先安装soapsdk3.0
下载地址:(http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe)
 2.然后成立二个ConsoleAlicaton程序

#include “stdafx.h”
#include <stdio.h>
#include <stdlib.h>

//先导入msxml4.dll,再导入mssoap30.dll
#import “msxml4.dll” 
#import “C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll” \
    exclude(“IStream”, “IErrorInfo”, “ISequentialStream”, “_LARGE_INTEGER”, \
    “_ULARGE_INTEGER”, “tagSTATSTG”, “_FILETIME”)
using namespace MSXML2;
using namespace MSSOAPLib30; 

void Add2(int No1,int No2)

{
    //char *_itoa( int value, char *string, int radix );

    char str1[10],str2[10];

    itoa(No1,str1,10); itoa(No2,str2,10);

    ISoapSerializerPtr Serializer;

    ISoapReaderPtr Reader;

    ISoapConnectorPtr Connector;    

    // Connect to the service.

    Connector.CreateInstance(__uuidof(HttpConnector30));   

    Connector->Property[“EndPointURL”] = “http://localhost:3542/Service.asmx“;   
    Connector->Connect();

    // Begin the message.

    Connector->Property[“SoapAction”] = “http://Fantasy/WebServiceDemo/Sub“;

    Connector->BeginMessage();      

    // Create the SoapSerializer object.

    Serializer.CreateInstance(__uuidof(SoapSerializer30));

    // Connect the serializer object to the input stream of the connector object.

    Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

    // Build the SOAP Message.

    Serializer->StartEnvelope(“”,””,””);

    //那里能够加上SoapHeader验证代码:

    /*

    Serializer->StartHeader(“”);

    Serializer->StartHeaderElement(“MyHeader”,”http://Fantasy/WebServiceDemo/",0,"","NONE“,””);   

    塞里alizer->StartElement(“UserName”,””,””,””);//填充参数2则失误。

    Serializer->WriteString(_bstr_t(userName));

    */

    Serializer->StartBody(“”);   

    //Serializer->StartElement(“AddNumbers”,”http://Fantasy/WebServiceDemo/“,””,””);
    Serializer->StartElement(“Sub”,”http://Fantasy/WebServiceDemo/“,””,””);

    //(第⑥个参数可有可无,若加上第一个参数出错!?)

    Serializer->StartElement(“NumberOne”,””,””,””);// NumberOne=

    Serializer->WriteString((_bstr_t)str1);

    Serializer->EndElement();      

    Serializer->StartElement(“NumberTwo”,””,””,””);// NumberOne= 

    Serializer->WriteString((_bstr_t)str2);

    Serializer->EndElement();

    Serializer->EndElement();  

    //

    Serializer->EndBody();

    Serializer->EndEnvelope();

    // Send the message to the XML Web service.   

    Connector->EndMessage();   

    // Read the response. 

    Reader.CreateInstance(__uuidof(SoapReader30));    

    // Connect the reader to the output stream of the connector object.  

    Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), “”);    

    // Display the result.

    printf(“Answer = %s\n”, (const char*)Reader->RpcResult->text);  

}

int main()

{   

    CoInitialize(NULL);   

    Add2(12,24);  

    CoUninitialize();

    return 0;

}

到此四个c++ 访问WebService的demo做完了。
参照网址:http://www.cnblogs.com/arui/archive/2006/06/15/426615.html
http://kimiya25.spaces.live.com/blog/cns!27A083D4FD9435E9!1852.entry?wa=wsignin1.0&sa=490902856
http://wpf-study.googlecode.com/files/VCInvokeWSDLDemo.rar

相关文章

网站地图xml地图