看流星社区

 找回密码
 注册账号
查看: 1874|回复: 0

vc6里调用WebService

[复制链接]

该用户从未签到

发表于 2017-6-3 14:26:22 | 显示全部楼层 |阅读模式
vc6里调用WebService

1、 安装soapsdk3.0开发包
(http://download.microsoft.com/download/2/e/0/2e068a11-9ef7-45f5-820f-89573d7c4939/soapsdk.exe)
2、 写代码
// WSWrapper.h: interface for the WSWrapper class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_WSWRAPPER_H__842F2D22_1EC1_4391_922D_C4E51CC65077__INCLUDED_)
#define AFX_WSWRAPPER_H__842F2D22_1EC1_4391_922D_C4E51CC65077__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#import "msxml4.dll"
#import "C:/Program Files/Common Files/MSSoap/Binaries/mssoap30.dll" exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
#include <string>
#include <Windows.h>
using namespace MSXML2;
using namespace MSSOAPLib30;
using std::string;

class WSWrapper
{
public:
WSWrapper(const char *wsURL, const char *wsNameSapce,const char *wsMethodName);
virtual ~WSWrapper();
string MisUserLogin(const string &amp;strUserName,const string &amp;strUserPassWord);
private:
const string _wsURL;
const string _wsNameSapce;
const string _wsMethodName;
};

#endif // !defined(AFX_WSWRAPPER_H__842F2D22_1EC1_4391_922D_C4E51CC65077__INCLUDED_)


// WSWrapper.cpp: implementation of the WSWrapper class.
//
//////////////////////////////////////////////////////////////////////

#include "WSWrapper.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

WSWrapper::WSWrapper(const char *wsURL, const char *wsNameSapce, const char *wsMethodName): _wsURL(wsURL), _wsNameSapce(wsNameSapce),_wsMethodName(wsMethodName)
{

}


WSWrapper::~WSWrapper()
{

}

string WSWrapper::MisUserLogin(const string &amp;strUserName,const string &amp;strUserPassWord)
{
try
{
HRESULT hr = CoInitialize(NULL);//初始化com环境
if(FAILED(hr))
{
//出错了
}

ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;

//连接到WebService
hr = Connector.CreateInstance(__uuidof(HttpConnector30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}

Connector->roperty["EndPointURL"] = _wsURL.c_str();
hr = Connector->Connect();
if(FAILED(hr))
{
//连接失败
return "error";
}
Connector->roperty["SoapAction"] = (_wsNameSapce + _wsMethodName).c_str();

//开始创建webservice的请求Soap包
hr = Connector->BeginMessage();
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
hr = Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
Serializer->StartEnvelope("SOAP", "http://schemas.xmlsoap.org/soap/envelope/", "");
Serializer->SoapAttribute("xsi", "", "http://www.w3.org/2001/XMLSchema-instance", "xmlns");
Serializer->SoapAttribute("xsd", "", "http://www.w3.org/2001/XMLSchema", "xmlns");

Serializer->StartBody(L"NONE");
Serializer->StartElement(_wsMethodName.c_str(), _wsNameSapce.c_str(), "NONE", "");
Serializer->StartElement(L"LoginName", _wsNameSapce.c_str(), "NONE", "");
Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
Serializer->WriteString(strUserName.c_str());
Serializer->EndElement();
Serializer->StartElement(L"assWord", _wsNameSapce.c_str(), "NONE", "");
Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
Serializer->WriteString(strUserPassWord.c_str());
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
hr = Connector->raw_EndMessage();
if(FAILED(hr))
{
//发送SOAP MESSAGE 失败
return "error";
}
hr = Connector->EndMessage();

//解析返回的soap包
hr = Reader.CreateInstance(__uuidof(SoapReader30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
VARIANT_BOOL bRead;
bRead = Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
string strResultXml((const char*)Reader->Body->xml);
string strResult;
if(Reader->GetRpcResult())
{
string strResult4 = (const char*)Reader->RpcResult->xml;
strResult = (const char*)Reader->RpcResult->text;
}
else
{
if(strResultXml.c_str()>0)
  {
strResult = "RpcNoResult And BodyHasResult";
  }
  else
  {
strResult = "RpcNoResult And BodyNoResult";
  }
}
return strResult;
}
catch(ISoapConnectorPtr Connector)
{
//got a exception
}
return "error";
}



3、 使用方法
//调用http://192.168.10.186:89/MisUserWS.asmx下的&ldquo;MIS_UserLogin&rdquo;WEB服务。
//查看WSDL内容 http://192.168.10.186:89/MisUserWS.asmx?WSDL
<?xml version="1.0" encoding="utf-8" ?>
- <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
- <s:element name="MIS_UserLogin">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="LoginName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="assWord" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="MIS_UserLoginResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MIS_UserLoginResult" type="tns:MisUserInfo" />
</s:sequence>
</s:complexType>
</s:element>
- <s:complexType name="MisUserInfo">
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="YPassWord" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="YUserName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkExePath" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="ID" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="upassword" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="utype" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="EmployeeID" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="IsLocked" type="s:boolean" />
<s:element minOccurs="1" maxOccurs="1" name="TeamID" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="LastLoginTime" type="s:dateTime" />
<s:element minOccurs="1" maxOccurs="1" name="DepartmentID" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="GroupID" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="Comm_Input_By" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="Comm_Input_Date" type="s:dateTime" />
<s:element minOccurs="1" maxOccurs="1" name="Comm_Modi_By" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="Comm_Modi_Date" type="s:dateTime" />
<s:element minOccurs="1" maxOccurs="1" name="RoleID" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="MachineIP" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkStartPath" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkEndPath" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Memory" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="SystemUser" type="s:string" />
</s:sequence>
</s:complexType>
- <s:element name="UserLogin">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="LoginName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="assWord" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="UserLoginResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="UserLoginResult" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="UserLoginOut">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="LoginName" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="UserLoginOutResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="UserLoginOutResult" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="MIS_Select_Userconfiguration">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="LoginID" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="MIS_Select_UserconfigurationResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MIS_Select_UserconfigurationResult" type="tns:MisUserInfo" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="MIS_Set_Userconfiguration">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MachineIP" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkStartPath" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkEndPath" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="SystemUser" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="WorkExePath" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="YUserName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="YPassWord" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="id" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="MIS_Set_UserconfigurationResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="MIS_Set_UserconfigurationResult" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
+ <wsdl:message name="MIS_UserLoginSoapIn">
<wsdl:part name="parameters" element="tns:MIS_UserLogin" />
</wsdl:message>
- <wsdl:message name="MIS_UserLoginSoapOut">
<wsdl:part name="parameters" element="tns:MIS_UserLoginResponse" />
</wsdl:message>
- <wsdl:message name="UserLoginSoapIn">
<wsdl:part name="parameters" element="tns:UserLogin" />
</wsdl:message>
- <wsdl:message name="UserLoginSoapOut">
<wsdl:part name="parameters" element="tns:UserLoginResponse" />
</wsdl:message>
- <wsdl:message name="UserLoginOutSoapIn">
<wsdl:part name="parameters" element="tns:UserLoginOut" />
</wsdl:message>
- <wsdl:message name="UserLoginOutSoapOut">
<wsdl:part name="parameters" element="tns:UserLoginOutResponse" />
</wsdl:message>
- <wsdl:message name="MIS_Select_UserconfigurationSoapIn">
<wsdl:part name="parameters" element="tns:MIS_Select_Userconfiguration" />
</wsdl:message>
- <wsdl:message name="MIS_Select_UserconfigurationSoapOut">
<wsdl:part name="parameters" element="tns:MIS_Select_UserconfigurationResponse" />
</wsdl:message>
- <wsdl:message name="MIS_Set_UserconfigurationSoapIn">
<wsdl:part name="parameters" element="tns:MIS_Set_Userconfiguration" />
</wsdl:message>
- <wsdl:message name="MIS_Set_UserconfigurationSoapOut">
<wsdl:part name="parameters" element="tns:MIS_Set_UserconfigurationResponse" />
</wsdl:message>
- <wsdl:portType name="MisUserWSSoap">
- <wsdlperation name="MIS_UserLogin">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">用户登录</wsdl:documentation>
<wsdl:input message="tns:MIS_UserLoginSoapIn" />
<wsdlutput message="tns:MIS_UserLoginSoapOut" />
</wsdlperation>
- <wsdlperation name="UserLogin">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">用户登录即保存信息到临时表里,判断是否重复登录:0用户不存在,1用户已登录,2临时表已存在并插入用户记录,3创建临时表并插入用户记录</wsdl:documentation>
<wsdl:input message="tns:UserLoginSoapIn" />
<wsdlutput message="tns:UserLoginSoapOut" />
</wsdlperation>
- <wsdlperation name="UserLoginOut">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">用户退出,即删除临时表记录.返回 0成功,-1 表中无此用户,-1 表不存在</wsdl:documentation>
<wsdl:input message="tns:UserLoginOutSoapIn" />
<wsdlutput message="tns:UserLoginOutSoapOut" />
</wsdlperation>
- <wsdlperation name="MIS_Select_Userconfiguration">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">查找用户初始配置信息</wsdl:documentation>
<wsdl:input message="tns:MIS_Select_UserconfigurationSoapIn" />
<wsdl:output message="tns:MIS_Select_UserconfigurationSoapOut" />
</wsdl:operation>
- <wsdl:operation name="MIS_Set_Userconfiguration">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">设置用户初始配置信息</wsdl:documentation>
<wsdl:input message="tns:MIS_Set_UserconfigurationSoapIn" />
<wsdl:output message="tns:MIS_Set_UserconfigurationSoapOut" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="MisUserWSSoap" type="tns:MisUserWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="MIS_UserLogin">
<soap:operation soapAction="http://tempuri.org/MIS_UserLogin" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="UserLogin">
<soap:operation soapAction="http://tempuri.org/UserLogin" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="UserLoginOut">
<soap:operation soapAction="http://tempuri.org/UserLoginOut" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="MIS_Select_Userconfiguration">
<soap:operation soapAction="http://tempuri.org/MIS_Select_Userconfiguration" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="MIS_Set_Userconfiguration">
<soap:operation soapAction="http://tempuri.org/MIS_Set_Userconfiguration" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="MisUserWSSoap12" type="tns:MisUserWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="MIS_UserLogin">
<soap12:operation soapAction="http://tempuri.org/MIS_UserLogin" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="UserLogin">
<soap12:operation soapAction="http://tempuri.org/UserLogin" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="UserLoginOut">
<soap12:operation soapAction="http://tempuri.org/UserLoginOut" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="MIS_Select_Userconfiguration">
<soap12:operation soapAction="http://tempuri.org/MIS_Select_Userconfiguration" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="MIS_Set_Userconfiguration">
<soap12:operation soapAction="http://tempuri.org/MIS_Set_Userconfiguration" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="MisUserWS">
- <wsdl:port name="MisUserWSSoap" binding="tns:MisUserWSSoap">
<soap:address location="http://192.168.10.186:89/MisUserWS.asmx" />
</wsdl:port>
- <wsdl:port name="MisUserWSSoap12" binding="tns:MisUserWSSoap12">
<soap12:address location="http://192.168.10.186:89/MisUserWS.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


//VC 调用WEB service
CString strResult = "",strWebSercivePath = "http://192.168.10.186:89/MisUserWS.asmx
",strNamespace = "http://tempuri.org/";
WSWrapper wsWrapper(strWebSercivePath,strNamespace,"MIS_UserLogin");
string strName = "admin";
string strPassword = "123";
string strResult = wsWrapper.MisUserLogin(strName,strPassword);

4、 注意事项
1)、查看WSDL内容,分析出WS的服务名(函数名)、参数列表。
2)、出现&ldquo;webservice 服务器未能识别 HTTP 头 SOAPAction 的值&hellip;&rdquo;问题,一般是由于命名空间不对,默认的命名空间是&ldquo;http://tempuri.org/&rdquo;。
3)、如果想分析详细的错误信息,可以查看  ISoapReaderPtr Reader; (const char*)Reader->Body->xml的内容,得到详细的信息,具体操作请参考上面的代码。
4)、(const char*)Reader->RpcResult->text 表示接受返回信息;(const char*)Reader->RpcResult->xml,表示接受得到信息,以XML格式存储。可以通过CMarkup类得到每个项的内容,可以映射到一个对应的结构体,从而得到所有返回的信息。
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-29 12:49

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表