怎样使用VC调用webservice的接口

该文章转载自:http://blog.chinaunix.net/uid-23766031-id-2386454.html

以下为内容:

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

网络上有很多这样的文章,但是无法一次性调试过,下面的程序完全是经过测试了的,可以使用,在这留档下。


代码如下:
/*begin*/
#include
#import "msxml4.dll"
using namespace MSXML2;

#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
            exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
                    "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错

void query(char *EndPointURL,char *NameSpace,char *method,int argv_len,char* argv[])
{

ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;

// Connect to the service

Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = EndPointURL;//接口位置
Connector->Connect();//和服务器连接

// Begin message
Connector->Property["SoapAction"] = _bstr_t(NameSpace)+"/"+_bstr_t(method);//方法名
Connector->BeginMessage();

Serializer.CreateInstance(__uuidof(SoapSerializer30));

// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

// 创建SOAP消息
Serializer->StartEnvelope("soap","","");
Serializer->StartBody("body");
Serializer->StartElement(method,NameSpace,"","");//命名空间必须有


for(int i=0;i
{
   Serializer->StartElement(argv[i],NameSpace,"","");
   Serializer->WriteString(argv[i]);
   Serializer->EndElement();
  
}

Serializer->EndElement();

Serializer->EndBody();
Serializer->EndEnvelope();

// Send the message to the web service
Connector->EndMessage();

// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));

// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

printf("Answer: %s\n",(const char*)Reader->RpcResult->xml);

}


int main(int argc, char* argv[])
{

CoInitialize(NULL);

    char *EndPointURL="接口地址";
    char *NameSpace="接口地址";
    char *method="call"; //接口提供的方法名
    int argv_len=4;
    char *argu[4]={"08SY20310982","100000", "error", "49"}; //参数,这里传递了4个,还可以增加
    query(EndPointURL,NameSpace,method,argv_len,argu);

    CoUninitialize();
    getchar();
    return 0;
/*end*/

你可能感兴趣的:(怎样使用VC调用webservice的接口)