C++通過msxml調用webservice樣本分享_C 語言

來源:互聯網
上載者:User

其實沒什麼難度,只是要調發送的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 = "http://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='http://www.jb51.net/soap/envelope/' xmlns:tem='http://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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.