String URL_LOGIN = "http://localhost:9080/foo/j_security_check"; String URL_DATA = "http://localhost:9080/foo/auth.html"; Client client = Client.create(); // add a filter to set cookies received from the server and to check if login has been triggered client.addFilter(new ClientFilter() { private ArrayList
String URL_LOGIN = "http://localhost:9080/foo/j_security_check"; String URL_DATA = "http://localhost:9080/foo/auth.html"; Client client = Client.create(); // add a filter to set cookies received from the server and to check if login has been triggered client.addFilter(new ClientFilter() { private ArrayList cookies; @Override public ClientResponse handle(ClientRequest request) throws ClientHandlerException { if (cookies != null) { request.getHeaders().put("Cookie", cookies); } ClientResponse response = getNext().handle(request); // copy cookies if (response.getCookies() != null) { if (cookies == null) { cookies = new ArrayList(); } // A simple addAll just for illustration (should probably check for duplicates and expired cookies) cookies.addAll(response.getCookies()); } return response; } }); String username = "me"; String password = "me"; // Get the protected web page: (this will make the server know that someone will try to access the protected resource) WebResource webResource = client.resource(URL_DATA); String response = webResource.get(String.class); // Login: webResource = client.resource(URL_LOGIN); com.sun.jersey.api.representation.Form form = new Form(); form.putSingle("j_username", username); form.putSingle("j_password", password); webResource.type("application/x-www-form-urlencoded").post(form); // Get the protected web page: (this time the service will return the data) webResource = client.resource(URL_DATA); response = webResource.get(String.class);