C++通過msxml調(diào)用webservice示例分享
其實(shí)沒什么難度,只是要調(diào)發(fā)送的xml格式,建議使用SoapUI調(diào)好,再粘到項(xiàng)目中
就是使用 msxml因?yàn)槭莔fc的東西,要在項(xiàng)目中設(shè)置在共享DLL中使用MFC
還有要在調(diào)用的服務(wù)后面加?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.dhdzp.com/")]
[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;
調(diào)用代碼
#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));//創(chuàng)建實(shí)列
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.dhdzp.com/soap/envelope/' xmlns:tem='http://www.dhdzp.com/'>");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
//sb.append("<tem:HelloWorld/>");//調(diào)用HelloWorld函數(shù)
sb.append("<tem:SayHello>");
sb.append("<tem:name>colin</tem:name>");//調(diào)用SayHello函數(shù),參數(shù)名是name,值為colin
sb.append("</tem:SayHello>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
xmlrequest->send(_variant_t(sb.c_str()));//發(fā)道數(shù)據(jù)
BSTR bstrbody;
xmlrequest->get_responseText(&bstrbody);//得到返回?cái)?shù)據(jù)
_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;
}
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(70.爬樓梯問題)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(70.爬樓梯問題),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
淺析string 與char* char[]之間的轉(zhuǎn)換
與char*不同的是,string不一定以NULL('\0')結(jié)束。string長度可以根據(jù)length()得到,string可以根據(jù)下標(biāo)訪問。所以,不能將string直接賦值給char*2013-10-10
C++多線程實(shí)現(xiàn)TCP服務(wù)器端同時(shí)和多個(gè)客戶端通信
通訊建立后首先由服務(wù)器端發(fā)送消息,客戶端接收消息;接著客戶端發(fā)送消息,服務(wù)器端接收消息,實(shí)現(xiàn)交互發(fā)送消息。本文主要介紹了C++多線程實(shí)現(xiàn)TCP服務(wù)器端同時(shí)和多個(gè)客戶端通信,感興趣的可以了解一下2021-05-05
Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼
本文主要介紹了Qt使用SqlLite實(shí)現(xiàn)權(quán)限管理的示例代碼,管理員針對不同人員進(jìn)行權(quán)限設(shè)定,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Visual Studio Code 2020安裝教程及CPP環(huán)境配置(教程圖解)
這篇文章主要介紹了Visual Studio Code 2020安裝教程、CPP環(huán)境配置,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C++使用遞歸方法求n階勒讓德多項(xiàng)式完整實(shí)例
這篇文章主要介紹了C++使用遞歸方法求n階勒讓德多項(xiàng)式,涉及C++遞歸算法與浮點(diǎn)數(shù)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
C++有限狀態(tài)機(jī)實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了C++有限狀態(tài)機(jī)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

