How to Properly Configure HTTP Sessions

This example code demonstrates how to configure your HTTP library for proper session management. Samples for popular Java libraries and Python are provided.

1. Java

1.1. Apache CXF

Always pass the same object instance to ClientProxy.getClient(Object) so that you get the same client instance back.


  Client client = ClientProxy.getClient(port);
  client.getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);

  HTTPConduit http = (HTTPConduit) client.getConduit();

1.2 AXIS2

Use the same ServiceClient instance throughout your code. A singleton pattern is appropriate (but not necessary).


  ServiceClient client = new ServiceClient();

1.3 Apache HTTP Client (4.2.1)

Use the same DefaultHttpClient instance (or whatever http client instance you create) for all of the connections in each thread. A singleton pattern is appropriate, but not necessary.


  TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
  X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
  ClientConnectionManager connection = null;
  try {
  SSLSocketFactory sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
      PlainSocketFactory psf = new PlainSocketFactory();
      Scheme http = new Scheme("http", 8080, psf);
      Scheme https = new Scheme("https", 8443, sslSf);
      SchemeRegistry schemeRegistry = new SchemeRegistry();
      connection = new PoolingClientConnectionManager(schemeRegistry);
  } catch (Exception e) {
      System.out.println("Unable to set up multiple scheme support in HttpClient connection manager.");
  DefaultHttpClient client = new DefaultHttpClient(connection);
  List<String> authPrefs = new ArrayList<String>();
  client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPrefs);
  client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

2. Python

Use the same opener instance throughout your code.


  import urllib2
  # Create an OpenerDirector with support for Basic HTTP Authentication...
  auth_handler = urllib2.HTTPBasicAuthHandler()
  auth_handler.add_password(realm='Cisco Web Services Realm',
  opener = urllib2.build_opener(auth_handler)
  # ...and install it globally so it can be used with urlopen.

New to UDS? Checkout these steps to get started.


Visit the UDS Developer Forums to ask questions and interact with other developers.