vc++ 调用web services

 转自:http://blog.sina.com.cn/s/blog_5417413401008wos.html

  vc6.0调用web services

下面是个控制台的样例
Toolkit3.0 终于给出VC6的样例了,1.0只能看到VB和ASP的

#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;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错


void Add()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));  

   Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl";  //这个当然得改成您自己的拉
   Connector->Connect();

   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/action/Sample1.AddNumbers";
   Connector->BeginMessage();

   // Create the SoapSerializer object.
   Serializer.CreateInstance(__uuidof(SoapSerializer30));

   // Connect the serializer object to the input stream of the connector object.
   Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

   // Build the SOAP Message.
   Serializer->StartEnvelope("","","");
   Serializer->StartBody("");
   Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","","");  //这是本地的Web Services,实际中要指定命名空间
   Serializer->StartElement("NumberOne","","","");
   Serializer->WriteString("5");
   Serializer->EndElement();
   Serializer->StartElement("NumberTwo","","","");
   Serializer->WriteString("10");
   Serializer->EndElement();
   Serializer->EndElement();
   Serializer->EndBody();
   Serializer->EndEnvelope();
  
   // Send the message to the XML Web service.
   Connector->EndMessage();     

   // Read the response.
   Reader.CreateInstance(__uuidof(SoapReader30));

   // Connect the reader to the output stream of the connector object.
   Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

   // Display the result.
   printf("Answer: %s/n", (const char*)Reader->RpcResult->text);
   
}

int main()
{
   CoInitialize(NULL);
   Add();
   CoUninitialize();
   return 0;
}

更改 EndPointURL 属性的值. 在URL里指定你的服务器名.

OK

总结一下必要的关键步骤
1.导入类型库

2.需要创建一个SoapConnector

3.下一步创建SoapSerializer

4.下一步把消息附加到SoapConnector的输入流

5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader,

6.SoapReader被连接到SoapConnector输出流

7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复

 

vc7.0 调用web services

//somehow the soap header dose not work now.

 

remember invoke Coinitialize(NULL)

 

add web services by right click the project and select add web reference.

 

CoInitialize(NULL); //initialize the com

 

CMemberServices *ms = new CMemberServices();;

int i;

DiskUsageInfo *_disk;

HRESULT hr;

CString username = "FZ566591079";

CString password = "1705649467";

CComBSTR bUserName, bPassword;

bUserName = username.AllocSysString();

bPassword = password.AllocSysString();

hr = ms->GetDiskUsage3(bUserName, bPassword, &_disk, &i);

if (FAILED(hr))

{

printf("Something happened when you call web services!");

}

else

 

{

printf("There are %d results returned", i);

}

::SysFreeString(bUserName);

::SysFreeString(bPassword);

delete ms;

return 0;

 

http://www.lokad.com/web-services-time-series-forecasting-tutorial-cpp.ashx

http://blog.tulipmm.cn/category/code/

ATL调用WebService倒是很简单,添加Web引用,好多代码就自动生成了。但是实际使用的时候遇到了一些问题:无论调用多少次Login,在调用其它方法的时候,服务器端查询Session总是为null。

还好我对Session机制也算了解,初步判断是客户端调用的时候并没有把Cookie内容放在HttpHeaders里面发回服务器——ATL类库并没有提供这样的实现。有两种方法可以解决这个问题:

1、修改WebService接口:在Login成功后返回SessionID到客户端,客户端每次调用后续方法的时候都在WebService方法的参数中传递这个SessionID;服务器端通过SessionID查找Session。这样的话就要现有的WebService就不能使用了,需要修改WebService增加新的方法,显然不是一个很好的解决办法。

2、修改客户端ATL类,使它可以保存Cookie。这样Web服务代码不需要修改,不会影响现有的其他客户端使用。

权衡得失,决定修权衡得失,决定修改ATL代码。研究发现添加Web引用后自动生成这样的代码

马上写了一个CAtlHttpClientT的子类重写这两个方法:

template <class TSocketClass>
class CCookieEnabledHttpClientT :
public CAtlHttpClientT<TSocketClass>
{
public:
CCookieEnabledHttpClientT(void)
{
}
public:
CString m_strCookies;

public:
bool Navigate(
const CUrl *pUrl,
ATL_NAVIGATE_DATA *pData
) throw(...)
{
CAtlNavigateData navData(*pData);
CString strHeaders = pData->szExtraHeaders;
strHeaders.Format(_T("%sCookie:%s/r/n"), strHeaders, m_strCookies);
navData.SetExtraHeaders(strHeaders);
return CAtlHttpClientT<TSocketClass>::Navigate(pUrl, &navData);
}

void OnSetCookie(LPCTSTR lpszStr) throw()
{
m_strCookies = lpszStr;
return;
}
};

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值