VC2008使用SOAPSDK3.0 访问WebService

vc2008 访问C# WebService

作者:Likwo  来源:博客园  发布时间:2009-09-23 17:49  阅读:390   原文链接   [收藏]  

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","");   

    Serializer->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++ 访问WebServicedemo做完了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值