As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.json.JSONObject; public class LoginAddLogoutTest { public String login() { String ticket = null; HttpClient client = new HttpClient(); String apiurl = "http://localhost:8080/alfresco/service/api/login"; PostMethod post = new PostMethod(apiurl); try { JSONObject login = new JSONObject(); login.put("username", "admin"); login.put("password", "admin"); post.setDoAuthentication(true); post.setRequestHeader("Content-Type", "application/json"); post.setRequestEntity(new StringRequestEntity(login.toString(), "application/json", "UTF-8")); int status = client.executeMethod(post); if (status != HttpStatus.SC_OK) { System.err.println("Method failed: " + post.getStatusLine()); } String responseData = post.getResponseBodyAsString(); System.out.println(responseData); JSONObject response = new JSONObject(responseData); ticket = response.getJSONObject("data").getString("ticket"); } catch (Exception e) { e.printStackTrace(); } finally { post.releaseConnection(); } return ticket; } public void logout(String ticket, String tologoutTicket) { HttpClient client = new HttpClient(); String apiurl = "http://localhost:8080/alfresco/service/api/login/ticket/" + tologoutTicket + "?alf_ticket=" + ticket; DeleteMethod post = new DeleteMethod(apiurl); try { post.setDoAuthentication(true); post.setRequestHeader("Content-Type", "application/xml"); int status = client.executeMethod(post); if (status != HttpStatus.SC_OK) { System.err.println("Method failed: " + post.getStatusLine()); } String resultString = post.getResponseBodyAsString(); System.out.println(resultString); } catch (Exception e) { e.printStackTrace(); } finally { post.releaseConnection(); } } public static void main(String[] args) { LoginAddLogoutTest t = new LoginAddLogoutTest(); String ticket = t.login(); t.logout(ticket, ticket); // in my case I logout admin using admin // authentication } }