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
  • 相关阅读:
    [Python][小知识][NO.3] Python 使用系统默认浏览器打开指定URL的网址
    [Python][小知识][NO.2] Python 字符串跨行连接,或拆分为多行显示
    [Python] wxPython 状态栏组件、消息对话框组件 学习总结(原创)
    [Python][小知识][NO.1] Python字符串前 加 u、r、b 的含义
    [Python] wxPython 编辑框组件学习总结 (原创)
    [Python] wxPython 菜单栏控件学习总结(原创)
    [Python] wxPython 基本控件 (转)
    HDU 2086 A1 = ? (找规律推导公式 + 水题)(Java版)
    HDU 1840 Equations (简单数学 + 水题)(Java版)
    UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)
  • 原文地址:https://www.cnblogs.com/likwo/p/1572813.html
Copyright © 2011-2022 走看看