×
Namespaces

Variants
Actions

Archived:Storing HTTP cookies using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): All (S60 3rd Edition)
Compatibility
Platform(s): S60 3rd Edition
S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 3rd Edition FP2
S60 3rd Edition FP1
S60 3rd Edition (initial release)
Article
Keywords: HTTP::ESetCookie, HTTP::ECookieValue
Created: User:Technical writer 1 (05 Dec 2008)
Last edited: hamishwillee (09 Aug 2012)

Description

The most common meaning of "cookie" on the Internet refers to a piece of information sent by a Web server to a client (browser) that the client is expected to store locally and send back to the server whenever making additional requests.

Depending on the type of the cookie it may be stored either for a short or a longer period of time. Cookies typically contain session-specific information such as login or registration details.

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.


Solution

The following code snippet demonstrates how a client can parse the HTTP response header for cookie information to be stored locally.

 _LIT8(KAccept, "*/*");
_LIT8(KAtext, "text/plain");
 
// Get request method string for HTTP GET
RStringF method =
iSession.StringPool().StringF(HTTP::EGET,RHTTPSession::GetTable());
iTransaction = iSession.OpenTransactionL(uri, *this, method);
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
SetHeaderL(hdr, HTTP::ECookie, KAtext);
SetHeaderL(hdr, HTTP::ECookieName, KAtext);
SetHeaderL(hdr, HTTP::ECookieValue, KAtext);
iTransaction.SubmitL();
 void CClientEngine::SetHeaderL(RHTTPHeaders aHeaders,
TInt aHdrField,
const TDesC8& aHdrValue)
{
RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue);
CleanupClosePushL(valStr);
THTTPHdrVal val(valStr);
aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField,
RHTTPSession::GetTable()), val);
CleanupStack::PopAndDestroy();
}
 
 
void CClientEngine::MHFRunL(RHTTPTransaction aTransaction,
const THTTPEvent& aEvent)
{
switch (aEvent.iStatus)
{
case THTTPEvent::EGotResponseHeaders:
{
RHTTPResponse resp1 = aTransaction.Response();
RStringPool strP = aTransaction.Session().StringPool();
RHTTPHeaders hdr = resp1.GetHeaderCollection();
THTTPHdrFieldIter it = hdr.Fields();
TBuf<512> fieldName16;
TBuf<512> fieldVal16;
 
while (it.AtEnd() == EFalse)
{
// Get the name of the next header field <br>
RStringTokenF fieldName = it();
RStringF fieldNameStr = strP.StringF(fieldName);
 
// Check it does indeed exist
THTTPHdrVal fieldVal;
if (hdr.GetField(fieldNameStr,0,fieldVal) == KErrNone)
{
RStringF wwwCookie = strP.StringF(
HTTP::ESetCookie, RHTTPSession::GetTable());
TInt mycookieCount = hdr.FieldPartsL(wwwCookie);
 
if (fieldNameStr == wwwCookie)
{
RStringF nameValStr;
RStringF valueValStr;
 
// Check the cookie name and value
RStringF name = strP.StringF(
HTTP::ECookieName, RHTTPSession::GetTable());
RStringF value = strP.StringF(HTTP::ECookieValue,
RHTTPSession::GetTable());
 
THTTPHdrVal nameVal, valueVal;
if (hdr.GetParam(wwwCookie, name, nameVal) == KErrNone)
{
nameValStr = strP.StringF(nameVal.StrF());
fieldName16.Copy(nameValStr.DesC());
}
if (hdr.GetParam(wwwCookie, value, valueVal) == KErrNone)
{
valueValStr = strP.StringF(valueVal.StrF());
fieldVal16.Copy(valueValStr.DesC());
}
 
...
// Create a cookie object with name, value and URI and save
// it as a file (for example, to app. private directory)
// URI can be obtained with:
// const TUriC8& requestUri = aTransaction.Request().URI();
}
}
// Advance the iterator
++it;
}
break;
}
...
}
}

See also

The above code can be used to modify the S60 Platform: HTTP Client API Example available on Nokia Developer.

This page was last modified on 9 August 2012, at 07:56.
76 page views in the last 30 days.
×