评论

收藏

[C++] c++ 连接webservice实例

编程语言 编程语言 发布于:2021-07-30 19:24 | 阅读数:247 | 评论:0

1、C#写的webservice接口文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebServiceDemo
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
2、C++客户端
#include "stdafx.h"
#import "./MSSOAP30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30; //前提是安装SOAP Toolkit3.0
#include <atlbase.h>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
HRESULT hr = Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://192.168.1.25/WebService1.asmx";
hr = Connector->Connect();
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
// 发送SOAP请求
Connector->Property["SoapAction"] = "http://tempuri.org/HelloWorld";
//开始SOAP消息
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30)); //创建SoapSerializer对象
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream)); //将SoapSerializer对象连接到Connector的输入字符串
// 创建SOAP消息
Serializer->StartEnvelope("soap", "", "");
Serializer->StartBody(""); //开始处理Body元素,参数为URI的编码类型,缺省为NONE
Serializer->StartElement("HelloWorld", "http://tempuri.org/", "", "soap");
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
hr = Connector->EndMessage(); //结束SOAP消息
// 读取SOAP响应信息
Reader.CreateInstance(__uuidof(SoapReader30)); //创建SoapReader对象
hr = Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), ""); //将SoapReader对象连接到Connector的输出字符串
wstring strResult;
Reader->RpcResult->get_text(&bstr);
strResult = (_bstr_t)bstr;
// Close Connect
Reader.Release();
Serializer.Release();
Connector.Release();
CoUninitialize();
return 0;
}
测试结果通过验证


关注下面的标签,发现更多相似文章