问题描述:
在用非.NET客户端调用WebService中,按照使用Soap Toolkit中的指导实现起来很简单,但在实际使用过程中却发现一个问题。
假如Webservice提供的方法是:int SimpleMath.Add(int n1,int n2),返回值是n1+n2, 但按照soap toolkit提供的例子,使用VC进行调用,得到的返回值却是0。
记录下我的解决过程,备忘。
试验环境:
OS:WindowsXP Professional
WebService:VS.NET 2003
WebService运行环境:IIS
客户端:VC6.0,VS.NET中的VC
SoapToolkit SDK:3.0
问题再现:
看Toolkit中稍微修改一下之后的例子代码:
#include <stdio.h>
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;
int test2()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service
Connector.CreateInstance(__uuidof(HttpConnector));
Connector->Property["EndPointURL"] = "http://localhost/WSTest/SimpleMath.asmx?WSDL";
Connector->Connect();
// Begin message
Connector->Property["SoapAction"] = "http://tempuri.org/Add";
Connector->BeginMessage();
// Create the SoapSerializer
Serializer.CreateInstance(__uuidof(SoapSerializer));
// Connect the serializer to the input stream of the connector
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message
Serializer->startEnvelope("","","");
Serializer->startBody("");
Serializer->startElement("Add","http://tempuri.org/","","");
Serializer->startElement("n1","","","");
Serializer->writeString("5");
Serializer->endElement();
Serializer->startElement("n2","","","");
Serializer->writeString("10");
Serializer->endElement();
Serializer->endElement();
Serializer->endBody();
Serializer->endEnvelope();
// Send the message to the web service
Connector->EndMessage();
// Let us read the response
Reader.CreateInstance(__uuidof(SoapReader));
// Connect the reader to the output stream of the connector
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// Display the result
printf("Answer: %s\n", (const char*)Reader->RPCResult->text);
return 0;
}
返回结果应该是15,但实际却返回了0。
在网上搜索发现有人提议把WebService的方法的参数改为string,然后在方法内部转换,我觉得这不是一个好办法。事实上,我经过发现,这样的实现问题会更多。
返回结果应该是15,但实际却返回了0。
在网上搜索发现有人提议把WebService的方法的参数改为string,然后在方法内部转换,我觉得这不是一个好办法。事实上,我经过发现,这样的实现问题会更多。