VC6 访问WebService的错误
在vc6里调用WebService,先安装soapsdk3.0,
源代码如下
string _wsURL = "http://127.0.0.1/WebServiceCom/Service1.asmx";
string _wsNameSapce = "http://127.0.0.1/WebServiceCom/";
string _wsMethodName = "Hello";
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->Property["EndPointURL"] = _wsURL.c_str();
Connector->Connect();
Connector->Property["SoapAction"] = ( _wsNameSapce + _wsMethodName).c_str(); //
//开始创建webservice的请求Soap包
Connector->BeginMessage();
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
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"strName", "", "NONE", "");
Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
Serializer->WriteString(strName.c_str());
Serializer->EndElement();
/*
Serializer->StartElement(L"strName2", "", "NONE", "");
Serializer->SoapAttribute("xsi:type", "", "xsd:string", "");
Serializer->WriteString(strName2.c_str());
Serializer->EndElement();
*/
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
//就这句会报错!
Connector->EndMessage();
//解析返回的soap包
hr = Reader.CreateInstance(__uuidof(SoapReader30));
if(FAILED(hr))
{
//创建com对象出错,一般是因为没有安装com
}
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
string strResult((const char*)Reader->RpcResult->text);
return strResult;
}
catch(...)
{
sss = GetLastError();
//got a exception
}
CoUninitialize();
return "error";
---------------------------------------
webService开在本机,工作正常,但是调用webService的接口Hello时,在Connector->EndMessage();出现异常。
请问是什么原因,怎么解决?