什么是Session, 什么是Cookie?
Session是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器生成一个唯一的SessionID,用该SessionID为标识符来存取服务器端的Session存储空间。而SessionID这一数据则是保存到客户端,用Cookie保存的,用户提交页面时,会将这一SessionID提交到服务器端,来存取Session数据。这一过程,是不用开发人员干预的。所以一旦客户端禁用Cookie,那么Session也会失效。
Cookie是客户端的存储空间,由浏览器来维持。
可以试一下,即使不写Cookie,在使用request.getCookies();取出的Cookie数组的长度也是1,而这个Cookie的名字就是JSESSIONID,还有一个很长的二进制的字符串,是SessionID的值。
实质上 URL 重写是通过向 URL 连接添加参数,并把 session ID 作为值包含在连接中。然而,为使这生效,你需要为你的 servlet 响应部分的每个连接添加 session ID.
Wap中如何进行Session管理?
Wap与PC不同之处在与Wap1.x不支持Session与Cookie,Wap2.0虽然支持但是只有协议支持还是不行,还要看手机终端支不支持?所以我们的解决方法就是完全不依赖Wap协议,完全不依赖手机终端,使用Url重写进行Session的维护,这个方法只与服务器端有关.
2.6.2 COOKIE规范
1. COOKIE一般情况下存放在用户的手机上,若用户手机不支持COOKIE功能,则WAP网关将协助存储用户COOKIE信息;
2. COOKIE可分为永久性COOKIE和暂时性COOKIE,通过手机与WAP网关的配合,两种COOKIE均可实现,可根据业务的需求选择使用;
3. 暂时性COOKIE的保存时间一般是1小时,可根据业务的需要自行指定;
4. 在游戏、社区类业务中,涉及用户身份识别的信息应使用永久性COOKIE;
------移动梦网WAP业务规范(V2.0)
hdr.SetFieldL(iSession.StringPool().StringF(HTTP::ECookie,
RHTTPSession::GetTable()), iCookieData);
读取cookiess 2rd
------------------------- read and save cookie from HTTP response HEADER ------------------
RHTTPHeaders aCurrentHeader;
aCurrentHeader = aResponse.GetHeaderCollection();
// 2. ------- set the RStringF string to get header field data --//
RStringF setcookie = iSession.StringPool().StringF(HTTP::ESetCookie,
RHTTPSession::GetTable());
// 3. ------- get the cookie raw data ---------------------------//
TInt ifcookie = aCurrentHeader.GetField(setcookie,0,iCookieData); //( THTTPHdrVal iCookieData;)
--------------------------
添加cookies
void CHTTPEngine::AddCookie(RHTTPHeaders aHeaders)
{
if(iCookie_name.Length() >0 && iCookie_value.Length() > 0)
{
RStringPool string_pool = iSession.StringPool();
RStringF cookie_rsf = string_pool.StringF(HTTP::ECookie,RHTTPSession::GetTable());
THTTPHdrVal field_value;
field_value.SetStrF (cookie_rsf);
aHeaders.SetFieldL(cookie_rsf, field_value);
THTTPHdrVal cookie_hdr;
RStringF cookie_name_rsf = string_pool.StringF(HTTP::ECookieName,RHTTPSession::GetTable());
RStringF name = string_pool.OpenFStringL (iCookie_name);
cookie_hdr.SetStrF (name);
aHeaders.SetParamL (cookie_rsf, cookie_name_rsf, cookie_hdr, 0);
name.Close();
RStringF cookie_value_rsf = string_pool.StringF(HTTP::ECookieValue,RHTTPSession::GetTable());
RStringF value = string_pool.OpenFStringL (iCookie_value);
cookie_hdr.SetStrF (value);
aHeaders.SetParamL (cookie_rsf, cookie_value_rsf, cookie_hdr, 0);
value.Close();
}
}
这段代码在第二版本中可以用
---------------------------------------------
问题已经解决
主要是字符格式问题
RStringF name = string_pool.OpenFStringL (iCookie_name);
cookie_hdr.SetStrF (name);
改成
RString name = string_pool.OpenStringL (iCookie_name);
cookie_hdr.SetStr (name);
下面的就可以通过了:
if(iCookie_name.Length() >0 && iCookie_value.Length() > 0)
{
RStringPool string_pool = iSession.StringPool();
RStringF cookie_rsf = string_pool.StringF(HTTP::ECookie,RHTTPSession::GetTable());
THTTPHdrVal field_value;
field_value.SetStrF (cookie_rsf);
aHeaders.SetFieldL(cookie_rsf, field_value);
THTTPHdrVal cookie_hdr;
RStringF cookie_name_rsf = string_pool.StringF(HTTP::ECookieName,RHTTPSession::GetTable());
RString name = string_pool.OpenStringL (iCookie_name);
cookie_hdr.SetStr (name);
aHeaders.SetParamL (cookie_rsf, cookie_name_rsf, cookie_hdr, 0);
name.Close();
RStringF cookie_value_rsf = string_pool.StringF(HTTP::ECookieValue,RHTTPSession::GetTable());
RString value = string_pool.OpenStringL (iCookie_value);
cookie_hdr.SetStr (value);
aHeaders.SetParamL (cookie_rsf, cookie_value_rsf, cookie_hdr, 0);
value.Close();
}
---------------------------------------------------
It's ok for 3rd.
void WriteCookie(RHTTPHeaders headers,TBuf<KMaxHeaderNameLen> _my_cookie_name,TBuf<KMaxHeaderValueLen> _my_cookie_value)
{
TBuf8<KMaxHeaderNameLen> my_cookie_name;
TBuf8<KMaxHeaderValueLen> my_cookie_value;
my_cookie_name.Copy(_my_cookie_name);
my_cookie_value.Copy(_my_cookie_value);
RStringPool string_pool = iSession.StringPool();
RStringF cookie_rsf = string_pool.StringF(HTTP::ECookie,RHTTPSession::GetTable());
THTTPHdrVal field_value;
field_value.SetStrF (cookie_rsf);
headers.SetFieldL(cookie_rsf, field_value);
THTTPHdrVal cookie_hdr;
RStringF cookie_name_rsf = string_pool.StringF(HTTP::ECookieName,RHTTPSession::GetTable());
RString name = string_pool.OpenStringL (my_cookie_name);
cookie_hdr.SetStr (name);
headers.SetParamL (cookie_rsf, cookie_name_rsf, cookie_hdr, 0);
name.Close();
RStringF cookie_value_rsf = string_pool.StringF(HTTP::ECookieValue,RHTTPSession::GetTable());
RString value = string_pool.OpenStringL (my_cookie_value);
cookie_hdr.SetStr (value);
headers.SetParamL (cookie_rsf, cookie_value_rsf, cookie_hdr, 0);
value.Close();
my_cookie_name.Zero();
my_cookie_value.Zero();
}
----------------------------
第二版中可用
To read a single cookie name and value from incoming headers:
HBufC8* my_cookie_name;
HBufC8* my_cookie_value;
RHTTPResponse resp = aTransaction.Response();
RHTTPHeaders headers = resp.GetHeaderCollection();
RStringPool string_pool = iSession.StringPool();
{
RStringF set_cookie_rsf = string_pool.StringF(HTTP::ESetCookie, RHTTPSession::GetTable());
THTTPHdrVal field_value;
if (headers.GetField(set_cookie_rsf, 0, field_value) == KErrNone)
{
RStringF cookie_rsf = string_pool.StringF(HTTP::ECookie, RHTTPSession::GetTable());
if (field_value.StrF() == cookie_rsf)
{
RStringF cookie_value_rsf = string_pool.StringF(HTTP::ECookieValue, RHTTPSession::GetTable());
RStringF cookie_name_rsf = string_pool.StringF(HTTP::ECookieName, RHTTPSession::GetTable());
THTTPHdrVal cookie_hdr;
if (!headers.GetParam(set_cookie_rsf, cookie_value_rsf, cookie_hdr))
{
RStringF rsf = string_pool.StringF(cookie_hdr.StrF());
my_cookie_value = rsf.DesC().AllocL();
}
if (!headers.GetParam(set_cookie_rsf, cookie_name_rsf, cookie_hdr))
{
RStringF rsf = string_pool.StringF(cookie_hdr.StrF());
my_cookie_name = rsf.DesC().AllocL();
}
}
}
}
To write a single cookie name and parameter into outgoing headers:
RStringPool string_pool = iSession.StringPool();
RStringF cookie_rsf = string_pool.StringF(HTTP::ECookie,RHTTPSession::GetTable());
THTTPHdrVal field_value;
field_value.SetStrF (cookie_rsf);
headers.SetFieldL(cookie_rsf, field_value);
THTTPHdrVal cookie_hdr;
RStringF cookie_name_rsf = string_pool.StringF(HTTP::ECookieName,RHTTPSession::GetTable());
RStringF name = string_pool.OpenFStringL (*my_cookie_name);
cookie_hdr.SetStrF (name);
headers.SetParamL (cookie_rsf, cookie_name_rsf, cookie_hdr, 0);
RStringF cookie_value_rsf = string_pool.StringF(HTTP::ECookieValue,RHTTPSession::GetTable());
RStringF value = string_pool.OpenFStringL (*my_cookie_value);
cookie_hdr.SetStrF (value);
headers.SetParamL (cookie_rsf, cookie_value_rsf, cookie_hdr, 0);