作者:陌-天佑_807 | 来源:互联网 | 2023-09-10 18:22
方法一:使用vs2005自带功能,添加web引用使用类似代码CoInitialize(NULL);HRESULThrS_OK;inthiResult;CComB
方法一:使用vs2005自带功能,添加web引用
使用类似代码
CoInitialize(NULL);
HRESULT hr = S_OK;
int hiResult;
CComBSTR username = Globle_User;
CComBSTR password = Globle_Pass;
CService* debug = new CService; // 代理对象
// 可以调用SetUrl动态设置Web服务地址
CString url;
url = L"http://" + NetAddress + L"/service.asmx?op=login";
debug->SetUrl(url);
hr = debug->login(username, password, &hiResult); //注意,返回值是以指针形式反回的
if(FAILED(hr))
{
}
delete debug;
CoUninitialize();
方法二:直接使用soap方法访问webservice
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
IDataEncoderFactoryPtr encoderFc;
HRESULT hr;
CoInitialize(NULL);
if(FAILED(Connector.CreateInstance(__uuidof(HttpConnector30)))) //创建对象
{
AfxMessageBox(L"soap失败");
return -1;
}
CString url = L"http://" + NetAddress + L"/service.asmx?wsdl";
Connector->Property["EndPointURL"] = (LPCTSTR)url;
Connector->Connect();
// Begin the message. //消息体
Connector->Property["SoapAction"] = "http://tempuri.org/login"; //函数体参数
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30));
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message.
try{
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
Serializer->StartElement("login","http://tempuri.org/","","");
Serializer->StartElement("ID","http://tempuri.org/","NONE","");
Serializer->WriteString((LPCTSTR)Globle_User);
Serializer->EndElement();
Serializer->StartElement("pass","http://tempuri.org/","NONE","");
Serializer->WriteString((LPCTSTR)Globle_Pass);
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
hr = Serializer->EndEnvelope();
Connector->EndMessage();
Reader.CreateInstance(__uuidof(SoapReader30));
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),""); //加载返回数据
----------------------------------------------------------------------
第一种方法跟踪调试进去发现也是使用soap,只是微软使用atlsoap给封装了,但为啥有的电脑无法使用第一种方法,第二种却可以?难道第一种方法中调用的函数缺少组件支持了?
不过第二种方法一般要安装soap开发包才可以正常调用,第一种则不用。
8 个解决方案
一个是生成包装类,都给你封装好了,你就像使用C++类的方式调用接口等
第二个是根据soap协议,调用对应的API来完成这个过程