1

I'm trying to Login in a .jsf intranet page, here is part of it:

     <form method="POST" action="j_security_check" name="loginForm" id="loginForm">
                <input name="j_username" type="text" class="textbox" maxlength="30"/>
                <input name="j_password" type="password" class="textbox" maxlength="30"/>
                <input type=submit value="Enter" class="button">
                <input type=submit value="Exit" class="button">
    </form>

I've searched and tryied something like this at java, but it didn't worked, I get the same page as result:

            HttpPost post = new HttpPost("http://xxxx/login.jsf");   

            List <NameValuePair> parameters = new ArrayList <NameValuePair>();   
            parameters.add(new BasicNameValuePair("j_username", "92232776"));   
            parameters.add(new BasicNameValuePair("j_password", "(7110oi14)")); 

            UrlEncodedFormEntity sendentity;

            sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);

            post.setEntity(sendentity);    
            HttpClient client = new DefaultHttpClient();   
            HttpResponse response = client.execute(post, httpContext);   

            System.out.print(convertInputStreamToString(response.getEntity().getContent()));   

(I'm using httpcomponents-client-4.2)

What I need to do to login in this page? is there anything I need to do with the code button "Send"?

Thank you guys.

CésarQ
  • 107
  • 1
  • 1
  • 6

1 Answers1

4

The login information is stored in the session. The session is backed by cookies. You need to maintain the cookies across the requests. Basically, you need to pass the retrieved Set-Cookie response header back on every subsequent HTTP request in the same session as long as the cookie is valid.

In HttpClient, you need to prepare a CookieStore which you set in the HttpContext which you in turn pass on every HttpClient#execute() call.

HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...

HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...

HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...

Please note that this j_security_check login step is not specifically related to JSF, it applies to every other Java Servlet based login form as well. Once you intend to submit a real JSF form as created by <h:form>, then you'd need to take a lot more into account. Head to this more detailed answer then: How can i programmatically upload a file to a website? Whilst this treats uploading of a file programmatically through a JSF form, the basic principles for submitting a JSF form are the same (certain hidden input fields have to be taken account with and so on).

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Why didn't you tell about that exception? That's completely unrelated to the login task. The `NullPointerException` just means that you're trying to access or invoke a reference which is `null`. The fix is quite simple: just make sure that it's not `null`, or do a `if` check for `null` and then bypass it. – BalusC Jul 02 '12 at 16:23
  • I'm getting the same login page as answer, even using cookies while executing the http'srequest... do you know what it could be? (no more nullpointer =D ) – CésarQ Jul 02 '12 at 19:44