zoukankan      html  css  js  c++  java
  • vc2008 访问C# WebService

    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++ 访问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
  • 相关阅读:
    排列 [计数dp]
    排列 [计数dp]
    函数 [计数]
    多态
    继承2
    2018年蓝桥杯b组国赛真题
    c++的继承
    运算符2
    运算符重载
    拷贝构造
  • 原文地址:https://www.cnblogs.com/likwo/p/1572813.html
Copyright © 2011-2022 走看看