Creating the HTTP
Connection in Java
The code fragment in this section
establishes an HTTP session with the vCenter Server and saves the HTTP session
cookie.
The following sequence describes these steps and
shows the corresponding objects and methods.
1 | 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(
)
|
2 | Get the VIM port. The VIM port
provides access to the vSphere API methods, including the
LoginByToken
method. | ![]() |
3 | Set the request context endpoint
address to the vCenter Server URL. | ![]() |
4 | Retrieve the ServiceContent. This
method establishes the HTTP connection. | ![]() |
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 SSO) and vCenter 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"); /* * 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);