• svc6 控制台程序利用SoapToolkit3.0调用WebService


    1. 首先要安装SoapToolkit3.0安装包并安装(我的安装目录为:C:Program FilesCommon Files)

    2. 新建vc控制台程序(空项目),项目名称:WinConsole6InvokeWebService,添加一个c++源文件(main.cpp)

    3。main.cpp源代码

    #include <stdio.h>
    #include <iostream>
    #include <vector>
     
    #import "msxml4.dll"
     
    using namespace std;
    using namespace MSXML2;
     
    #import "C:Program FilesCommon FilesMSSoapBinariesMSSOAP30.dll" 
            exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", 
            "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
     
    using namespace MSSOAPLib30;
     
    void query(char* EndPointURL, char* Namespace, char* method, vector<string>& v)
    {
        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(vector<string>::iterator it = v.begin(); it != v.end(); it++)
        {
            Serializer->StartElement("username", Namespace, "", "");
            Serializer->WriteString(it->c_str());
            Serializer->EndElement();
        }
     
        Serializer->EndElement();
     
        Serializer->EndBody();
        Serializer->EndEnvelope();
     
        Connector->EndMessage();             // Send the message to the web service
     
        // 读取响应
        Reader.CreateInstance(__uuidof(SoapReader30));
        Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
     
        printf("Answer: %s
    ", (const char*)Reader->RpcResult->text); // Reader->RpcResult->Gettext()等效
    }
     
    int main(int argc, char* argv[])
    {
        CoInitialize(NULL);
        char* EndPointURL = "http://192.168.0.100/WebService1/Service.asmx";
        char* Namespace = "http://tempuri.org/";
     
        vector<string> v1, v2;
     
        v2.push_back("JoeBlack");
        query(EndPointURL, Namespace, "Hello", v2);
     
        CoUninitialize();
        getchar();
     
        return 0;
    }
    

      

    3>通过vs2010发布服务, 添加webservices,文件名Service.asmx,不能通过wcf发布,否则上面的代码回报错。

    Service.asmx文件源码

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
     
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        public Service () {
     
            //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }
     
        [WebMethod(Description = "Let's say "Hi"")]
        public string Hi()
        {
            return "Hello World, Happy New Year!";
        }
     
        [WebMethod(Description = "Hello JoeBlack")]
        public string Hello(string username)
        {
            return username + ", Happy New Year!";
        }
     
        [WebMethod(Description = "求和的方法")]
        public double addition(double i, double j)
        {
            return i + j;
        }
     
        [WebMethod(Description = "求差的方法")]
        public double subtract(double i, double j)
        {
            return i - j;
        }
     
        [WebMethod(Description = "求积的方法")]
        public double multiply(double i, double j)
        {
            return i * j;
        }
     
        [WebMethod(Description = "求商的方法")]
        public double division(double i, double j)
        {
            if (j != 0)
                return i / j;
            else
                return 0;
        }
    }
    

      程序员的基础教程:菜鸟程序员

  • 相关阅读:
    大数据技术栈,主要有哪些
    微服务海量日志监控平台
    Elastic APM安装
    Elasticsearch SSL认证/证书制作
    Elastic APM 上报数据分析与应用
    elasticsearch7.X x-pack破解
    Netty源码学习系列之5-NioEventLoop的run方法
    Netty源码学习系列之4-ServerBootstrap的bind方法
    Netty源码学习系列之3-ServerBootstrap的初始化
    Netty源码学习系列之2-NioEventLoopGroup的初始化
  • 原文地址:https://www.cnblogs.com/guohu/p/5587509.html
Copyright © 2020-2023  润新知