The following code fragment sets up the HTTP connection with the vCenter Server instance.

  1. Retrieve the VimPort interface. This provides access to the vSphere API methods.
  2. Retrieve the request context and set the vCenter Server endpoint address in the request context.
  3. Set the session cookie in the request context. The cookie (cookieVal) is obtained from the vCenter LoginByToken Example.
  4. Call the RetrieveServiceContent method to establish the HTTP connection with the vCenter Server instance.

vCenter Server Connection

// 1. Retrieve the VimPort interface.
vimService = new VimService();
vimPort = vimService.getVimPort();

// 2. Retrieve the request context and set the vCenter Server endpoint.
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vcurl.toString());
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

// 3. Put the extracted vCenter session cookie into the VimPortType request header.
Map<String, List<String>> headers =
     (Map<String, List<String>>) ctxt.get(MessageContext.HTTP_REQUEST_HEADERS);
if (headers == null) {
     headers = new HashMap<String, List<String>>();
}
headers.put("Cookie", Arrays.asList(cookieVal));
ctxt.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

// 4. Retrieve the vCenter Server service content. (Establishes the HTTP connection)
vimServiceContent = vimPort.retrieveServiceContent(this.getVimServiceInstanceReference());