欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

C++通过msxml调用webservice示例分享

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

其实没什么难度,只是要调发送的xml格式,建议使用SoapUI调好,再粘到项目中

就是使用 msxml因为是mfc的东西,要在项目中设置在共享DLL中使用MFC

还有要在调用的服务后面加?wsdl解释成xml格式

代码

webservice

复制代码 代码如下:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "//www.jb51.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string SayHello(string name)
        {
            return "Hello "+name;
        }
    }
}

头文件

[code]
#pragma once
#include "stdafx.h"
#include "Atlbase.h"
//#import "msxml.dll"
#import "msxml4.dll"

using namespace MSXML2;
#include <string>
#include <iostream>
using namespace std;

调用代码

复制代码 代码如下:

#include "Main.h"
int main(int argc, char* argv[])
{
    printf("Test of XMLHTTP by masterz!\n");
    CoInitialize(NULL);
    try
    {
        IXMLHTTPRequestPtr xmlrequest;//定义http请求对象
        xmlrequest.CreateInstance(__uuidof(XMLHTTP));//创建实列
        CComVariant vFalse(FALSE);
        CComVariant vNull(NULL);
        xmlrequest->open("POST",bstr_t("http://192.168.71.172/Service1.asmx?wsdl"),vFalse,vNull,vNull);//打开WEBServeice方法:加?wsdl
        xmlrequest->setRequestHeader(_bstr_t(_T("Content-Type")), _bstr_t(_T("text/xml")));

  string sb;
  sb.append("<?xml version='1.0' encoding='utf-8'?>");
  sb.append("<soapenv:Envelope xmlns:soapenv='//www.jb51.net/soap/envelope/' xmlns:tem='//www.jb51.net/'>");
  sb.append("<soapenv:Header/>");
  sb.append("<soapenv:Body>");

  //sb.append("<tem:HelloWorld/>");//调用HelloWorld函数

  sb.append("<tem:SayHello>");
  sb.append("<tem:name>colin</tem:name>");//调用SayHello函数,参数名是name,值为colin
  sb.append("</tem:SayHello>");

  sb.append("</soapenv:Body>");
  sb.append("</soapenv:Envelope>");

  xmlrequest->send(_variant_t(sb.c_str()));//发道数据

        BSTR bstrbody;
        xmlrequest->get_responseText(&bstrbody);//得到返回数据
        _bstr_t bstrtbody(bstrbody);
        printf("%s\n",(LPCTSTR)bstrtbody);
        MSXML2::IXMLDOMDocument2Ptr m_xmldoc;
        m_xmldoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
        m_xmldoc->loadXML(bstrbody);
        MSXML2::IXMLDOMNodePtr node = m_xmldoc->documentElement->firstChild;

        LPCTSTR str = (LPCTSTR)node->nodeName;
        string str2=(string)m_xmldoc->documentElement->text;
        cout<<str2<<endl;
    }
    catch (_com_error &e)
    {
        printf("Description = '%s'\n", (char*) e.Description());
    }
    CoUninitialize();
    printf("program end\n");
    return 0;
}

上一篇:c语言main函数使用及其参数介绍

栏    目:C语言

下一篇:简单的socket编程入门示例

本文标题:C++通过msxml调用webservice示例分享

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3719.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有