webservice 使用vs2005生成代理

来源:互联网 发布:大富翁数据 编辑:程序博客网 时间:2024/05/22 18:32

 使用类似代码
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),"");  //加载返回数据

0 0
原创粉丝点击