Symbian学习笔记8 之 初探WebServices API的使用(上)

 (转载 --留着以后学习)

从SDK文档中提供的资料来看这个接口似乎有点复杂,包括了Connection API、Description API和Manager API三套东西,此外还涉到了XML的解析之类的一些API的应用。

阅读了一下它的例子程序(S60Ex目录下的AddressBook),让我更晕乎了。怎么跟自己平时使用的WebService不一样了?

在SDK文档中关于CSenServiceConnection有这么一段描述:

Web Services包括两种不同的框架模型: 1. Identity Based Web Services Framework (ID-WSF). The framework ID for this is KDefaultIdWsfFrameworkID  ("ID-WSF"). 2. Basic Web Services Framework. Framework ID is KDefaultBasicWebServicesFrameworkID ("WS-I").

如果提供了Contract则缺省使用ID-WSF。

首先用.NET做一个简单的WebServices来测试,就用缺省产生的HelloWorld吧。很简单的,它的SOAP描述如下:
view plaincopy to clipboardprint?
<PRE class=csharp name="code">POST /uim/PService.asmx HTTP/1.1     

Host: localhost   

Content-Type: text/xml; charset=utf-8     

Content-Length: length     

SOAPAction: "urn:pservice:helloworld"   

<?xml version="1.0" encoding="utf-8"?>   

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   
<soap:Body>   

<HelloWorld xmlns="http://sharetop/pservice" />   

</soap:Body>   

</soap:Envelope>   

HTTP/1.1 200 OK     

Content-Type: text/xml; charset=utf-8     

Content-Length: length     

<?xml version="1.0" encoding="utf-8"?>   

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   

  <soap:Body>   

<HelloWorldResponse xmlns="http://sharetop/pservice">   
<HelloWorldResult>string</HelloWorldResult>   
  </HelloWorldResponse>   

  </soap:Body>   

</soap:Envelope></PRE>  

view plaincopy to clipboardprint?

POST /uim/PService.asmx HTTP/1.1     
Host: localhost
Content-Type: text/xml; charset=utf-8     

Content-Length: length     

SOAPAction: "urn:pservice:helloworld"   

<?xml version="1.0" encoding="utf-8"?>   

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">     

<soap:Body>     

<HelloWorld xmlns="http://sharetop/pservice" />     

</soap:Body>     

</soap:Envelope>     

HTTP/1.1 200 OK     

Content-Type: text/xml; charset=utf-8     

Content-Length: length   

<?xml version="1.0" encoding="utf-8"?>     

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">     

  <soap:Body>     

<HelloWorldResponse xmlns="http://sharetop/pservice">     

<HelloWorldResult>string</HelloWorldResult>     

</HelloWorldResponse>     

</soap:Body>   

</soap:Envelope>  

POST /uim/PService.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "urn:pservice:helloworld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorld xmlns="http://sharetop/pservice" />
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HelloWorldResponse xmlns="http://sharetop/pservice">
      <HelloWorldResult>string</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

下面我们自己来做一个WS的客户端实例吧。先用向导生成一个HelloWorld应用,为了研究方便,我们不打算做什么界面,所有的输出都通过LOG输出到日志文件。

为了编码方便,我们增加一个类WebEngine,它应该派生于CSenBaseFragment和MSenServiceConsumer。声明如下:
view plaincopy to clipboardprint?

class CWebEngine : public CSenBaseFragment, public MSenServiceConsumer     

{     

public:   

  ~CWebEngine();     
static CWebEngine* NewL();     
  static CWebEngine* NewLC();     

void ConnectL();   

void SayHello();     

   //from MSenServiceConsumer   

  virtual void HandleMessageL(const TDesC8& aMessage);   

   virtual void HandleErrorL(const TInt aErrorCode,const TDesC8& aError);   

  virtual void SetStatus(const TInt aStatus);     

protected:     

//from CSenBaseFragment   

virtual void StartElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName, const Xml::RAttributeArray& aAttrs);  

  virtual void EndElementL(const TDesC8& aNsUri,  const TDesC8& aLocalName, const TDesC8& aQName);   

private:   

CWebEngine();   

void ConstructL();     

public:   

CHelloWorldResult * delegate;   

private:     

  CSenServiceConnection* iConnection;   

CSenXmlServiceDescription* iSession;     

CSenXmlReader* iXmlReader;     

};  

class CWebEngine : public CSenBaseFragment, public MSenServiceConsumer

{

public:

        ~CWebEngine();

        static CWebEngine* NewL();

        static CWebEngine* NewLC();

        void ConnectL();

        void SayHello();

        //from MSenServiceConsumer

        virtual void HandleMessageL(const TDesC8& aMessage);

        virtual void HandleErrorL(const TInt aErrorCode,const TDesC8& aError);

        virtual void SetStatus(const TInt aStatus);

protected:

        //from CSenBaseFragment

        virtual void StartElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName, const Xml::RAttributeArray& aAttrs);                  

        virtual void EndElementL(const TDesC8& aNsUri,  const TDesC8& aLocalName, const TDesC8& aQName);

private:

        CWebEngine();

        void ConstructL();

public:

        CHelloWorldResult * delegate;

private:

        CSenServiceConnection* iConnection;

        CSenXmlServiceDescription* iSession;       

        CSenXmlReader* iXmlReader;

};

除了实现两个父类的方法以外,还要增加ConnectL()用来连接,SayHello()用来调用远程方法。那个delegate是一个 CHelloWorldResult类的实例,这个类同样派生于CSenDomFragment,说明它对应一段XML内容,我们用它来处理结果,就是那个HelloWorldResponse标签下的内容。

这个WebEngine的实现逻辑是:先在ConnectL中初始化WS客户端,在SetStatus回调中取当前状态值如果为 KSenConnectionStatusReady ,则可以调用SayHello去执行那个WS的方法,然后,在HandleMessageL回调中将得到的结果(XML内容的字节流)去解析一下,解析 XML的回调就是那两个StartElement和EndElement。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值