I'm having difficulty getting a jersey 2.18 client to perform a form-based
login. I know the basics are correct (url, username, password) because I
can get this to work using curl.
The first problem is that I don't seem to be getting a cookie back from the
service when I post to the login form. Either that or I'm looking for the
cookie the wrong way.
Here is the code that attempts to login:
* public void doLogin() {*
* WebTarget myTarget = client.target("
http://localhost:8080/myapp
<
http://localhost:8080/myapp>").path("/login.xhtml/j_security_check");*
* Form form = new Form();*
* form.param("j_username", "admin");*
* form.param("j_password", "admin");*
* Response response = myTarget*
* .request(MediaType.APPLICATION_FORM_URLENCODED_TYPE)*
* .post(Entity.form(form));*
* System.out.println(String.format("There are %d cookies",
response.getCookies().size()));*
* }*
The output is always "There are 0 cookies". I would think there should be
one?
I need to solve this problem before the next, which is how do I attach the
cookie along with every subsequent request? Would this approach work:
public class MyClientRequestFilter implements ClientRequestFilter {
public static List<Object> cookies=null;
@Override
public void filter(ClientRequestContext arg0) throws IOException {
System.out.println("req filter " + arg0.getUri().toString());
if (cookies != null) {
arg0.getHeaders().put("Cookie", cookies);
}
}
}