Try OpenEdge Now
skip to main content
Programming Interfaces
Input/Output Processes : Making HTTP(S) requests from ABL applications : Implementing stateful clients with cookies
 

Implementing stateful clients with cookies

The default HTTP client is stateless (i.e. without cookies). OpenEdge.Net.Http.StatefulHttpClient extends the standard HTTP client and writes cookies into the IHttpRequest and reads them from the IHttpResponse after request execution.
The OpenEdge.Net.HTTP.ICookieJar interface controls access to and from the cookie jar. Cookies are stored in an instance of an OpenEdge.Net.HTTP.ICookieJar class. The standard implementation is OpenEdge.Net.HTTP.CookieJar which writes persistent cookies to disk in JSON form.
The rules for storing cookies are:
*Cookies must be associated with a domain.
*If the cookie itself has no domain, use the request's host domain.
*If the cookie has a domain, use that. This domain may be prefixed with a wildcard '.' .
*The cookie domain and the request host domain must be the same.
*Cookies that have no expiration date are considered session cookies and are deleted when the CookieJar is deleted/cleaned up. Session cookies can be cleaned with the CleanSessionCookies() method.
The rules for retrieving cookies for a request are:
*The request's host is identical to cookie's domain, or the host matches the cookie's domain.
*The request's URL path begins with the cookie path.

Examples

The following is a general example of implementing cookies in order to establish a stateful client.
Implementing cookies
USING OpenEdge.Net.HTTP.IHttpClient.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.ICookieJar.
USING OpenEdge.Net.HTTP.CookieJarBuilder.
USING OpenEdge.Net.HTTP.Cookie.

DEFINE VARIABLE moHttpClient AS IHttpClient NO-UNDO.

moHttpClient = ClientBuilder:Build()
:KeepCookies(CookieJarBuilder:Build():CookieJar)
:Client.
To read/write cookies on requests and responses, use the methods from the IHttpMessage interface as shown in the following code sample.
Read/write methods for cookies
/** Adds a cookie to this message
@param Cookie The cookie to add. */

METHOD PUBLIC VOID SetCookie(INPUT poCookie AS Cookie).

/** Returns all the cookies for this message
@param Cookie[] An array of cookies
@return integer The number of cookies returned */

METHOD PUBLIC INTEGER GetCookies(OUTPUT poCookies AS Cookie extent).

/** Removes a cookie from this message
@param Cookie The cookie to remove. */

METHOD PUBLIC VOID RemoveCookie(INPUT poCookie AS Cookie).

/** Indicates whether a cookie exists for this message
@param Cookie The cookie to check
@return logical True if this message contains the cookie */

METHOD PUBLIC LOGICAL HasCookie(INPUT poCookie AS Cookie).

/** Removes all cookies from this message */

METHOD PUBLIC VOID ClearCookies().
Note: You can inspect an individual cookie by simply calling ToString() on it (STRING(oCookie) or MESSAGE oCookie also work. )