Saving the vCenter Server Session Cookie
Last Updated December 19, 2024

The code fragment in this section establishes an HTTP session with the
instance and saves the HTTP session cookie.
The following sequence describes these steps and shows the corresponding objects and methods.
Use the
getHandlerResolver
method to save the default message handler. To use the HTTP and SOAP message handlers, you must first save the default message handler so that you can restore it after login. The HTTP and SOAP message handlers impose overhead that is unnecessary after login.
VimService.getHandlerResolver( )
Set the cookie handler. The
HeaderCookieExtractionHandler
method retrieves the HTTP cookie.
Get the VIM port. The VIM port provides access to the vSphere API methods, including the
LoginByToken
method.
Set the request context endpoint address to the vCenter Server URL.
Retrieve the
ServiceContent
. This method establishes the HTTP connection and sets the session cookie.
Extract the cookie and save it for later use.
HeaderCookieExtractionHandler.getCookie ( )
The following example shows Java code that saves the session cookie.
Saving the vCenter Server Session Cookie
/*
 * The example uses a SAML token (obtained from a vCenter Single Sign-On Server)
 * and the vCenter Server URL.
 * The following declarations indicate the datatypes; the token datatype (Element) corresponds
 * to the token datatype returned by the vCenter Single Sign-On Server.
 *
 * Element token;       -- from vCenter Single Sign-On Server 
 * String vcServerUrl;  -- identifies vCenter Server 
 *
 * First, save the default message handler.
 */

HandlerResolver defaultHandler = vimService.getHandlerResolver();

/*
 * Create a VIM service object.
 */
vimService = new VimService();

/*
 * Construct a managed object reference for the ServiceInstance.
 */
ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
SVC_INST_REF.setType("ServiceInstance");
SVC_INST_REF.setValue("ServiceInstance");

/*
 * Create a handler resolver.
 * Create a cookie extraction handler and add it to the handler resolver.
 * Set the VIM service handler resolver.
 */
HeaderCookieExtractionHandler cookieExtractor = new HeaderCookieExtractionHandler();
HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
handlerResolver.addHandler(cookieExtractor);
vimService.setHandlerResolver(handlerResolver);

/*
 * Get the VIM port for access to vSphere API methods. This call clears the request context.
 */
vimPort = vimService.getVimPort();

/*
 * Get the request context and set the connection endpoint.
 */
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vcServerUrl);
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

/*
 * Retrieve the ServiceContent. This call establishes the HTTP connection.
 */
serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);

/*
 * Save the HTTP cookie.
 */
String cookie = cookieExtractor.getCookie();