Designer 10.7 | webMethods Mobile Development | Code Snippets | Configuring a Session Object with Credentials
 
Configuring a Session Object with Credentials
When using RESTful services, it is often required to send authentication information with the HTTP header. This can be achieved using the Session object. A session is used to manage cookies and user credentials across different HTTP requests. Only one session exists at a time.
Use the Session object to add credentials to be automatically applied to any RESTful service.
import com.softwareag.mobile.runtime.toolkit.rest.Credentials;
import com.softwareag.mobile.runtime.toolkit.rest.Session;

final Session session = Session.get();
final Credentials credentials = new Credentials("user", "password");
credentials.addHostname("hostname");
credentials.addProtocol("http");
credentials.addRealm("realm");

session.addCredentials(credentials);
Setting credentials this way will ensure that each request uses these credentials. By default, credentials will be added to the request header as HTTP basic authentication.
It is also possible to delete a session. Be aware that all credentials and cookies set before destroying will be lost. If there is currently no session, executing a request will create one.
final Session session = Session.get();
session.destroy();
Related Topics