现在的位置: 首页 > 综合 > 正文

用Symbian中读多Cookie值(Reading Multiple Http Cookies using Symbian C++)

2013年09月19日 ⁄ 综合 ⁄ 共 3802字 ⁄ 字号 评论关闭

Overview

Symbian cookies are stored in the http header collection like other header information, but can be tricky to retrieve. Attempting to gather them the standard way returns a single item whose name is "Set-cookie" and whose value is "Cookie". The following code snippet shows how to get multiple cookies from an HTTP transaction. The code is tested with S60 3rd Edition FP1 and FP2.

Symbian的cookie保存在http的header集中的,和其它的header信息一样,但是可以取值可能会有麻烦。企图收集他们的标准方法是返圆一个命名为"Set-cookie"的单独项,他的值是"Cookie".下面的代码片断展示了怎样从HTTP transaction中取多个cookie的办法。这个代码在S60 3版的FP1和FP2中都测试过。(我在第三版下也测试通过)。

The S60 implementation stores the cookie in a private folder of the Cookie Manager component. If a 3rd party wants to access these cookie files directly, AllFiles capability is required.

S60中实现存储cookie到Cookie管理组件的private文件夹中,如果第三方想要直接的访问这些cookie文件,那么他需要所有的文件权限。

 

  1. void loadFromTransaction(RHTTPTransaction& aTrans)  
  2. {  
  3.     RHTTPResponse resp = aTrans.Response();  
  4.     RStringPool strP = aTrans.Session().StringPool();  
  5.     RHTTPHeaders headers = resp.GetHeaderCollection();  
  6.     THTTPHdrFieldIter it = headers.Fields();  
  7.    
  8.     while (it.AtEnd() == EFalse)  
  9.     {  
  10.         RStringTokenF fieldName = it();  
  11.         RStringF fieldNameStr = strP.StringF(fieldName);  
  12.         THTTPHdrVal fieldVal;  
  13.    
  14.         if (headers.GetField(fieldNameStr, 0, fieldVal) == KErrNone)  
  15.         {  
  16.             RStringF wwwCookie = strP.StringF(HTTP::ESetCookie, RHTTPSession::GetTable());  
  17.             if (fieldNameStr == wwwCookie)  
  18.             {  
  19.                 RStringF nameValStr;  
  20.                 RStringF valueValStr;  
  21.    
  22.                 // Check the cookie name and value  
  23.                 RStringF name = strP.StringF(HTTP::ECookieName, RHTTPSession::GetTable());  
  24.                 RStringF value = strP.StringF(HTTP::ECookieValue, RHTTPSession::GetTable());  
  25.    
  26.                 TInt numCookies = headers.FieldPartsL(wwwCookie);  
  27.    
  28.                 THTTPHdrVal nameVal, valueVal;  
  29.                 for (int i = 0; i < numCookies; i++)  
  30.                 {  
  31.                     string nameFromCookie = "", valueFromCookie = "";  
  32.    
  33.                     if (headers.GetParam(wwwCookie, name, nameVal, i) == KErrNone)  
  34.                     {  
  35.                         RStringF strF;  
  36.                         RString str;  
  37.                         if (nameVal.Type() == THTTPHdrVal::KStrFVal)  
  38.                         {  
  39.                             strF = nameVal.StrF();  
  40.                             nameValStr = strP.StringF(strF); // this is the cookie's name  
  41.                         }  
  42.                         else if (nameVal.Type() == THTTPHdrVal::KStrVal)  
  43.                         {  
  44.                             str = nameVal.Str(); // this is the cookie's name  
  45.                         }  
  46.                     }  
  47.                     if (headers.GetParam(wwwCookie, value, valueVal, i) == KErrNone)  
  48.                     {  
  49.                         RStringF strF;  
  50.                         RString str;  
  51.                         if (valueVal.Type() == THTTPHdrVal::KStrFVal)  
  52.                         {  
  53.                             strF = valueVal.StrF();  
  54.                             valueValStr = strP.StringF(strF); // this is the cookie's value  
  55.                         }  
  56.                         else if (valueVal.Type() == THTTPHdrVal::KStrVal)  
  57.                         {  
  58.                             str = valueVal.Str(); // this is the cookie's value  
  59.                         }  
  60.                     }  
  61.    
  62.                     /*** 
  63.                      * do something with the name and value, such as storing them 
  64.                      * locally or in a file 
  65.                      ***/  
  66.                 } // end for loop  
  67.             }  
  68.         }  
  69.         ++it;  
  70.     } // end while  
  71. // end loadFromTransaction  

 

原文地址:http://wiki.forum.nokia.com/index.php/Reading_Multiple_Http_Cookies_using_Symbian_C%2B%2B

抱歉!评论已关闭.