I'm using Apache's HttpCommons 3.1 package to login to some website.

The web site is jsp page using the form using j_security_check together with j_username and j_password.

From the browser, I'd perform the following:

  1. o towards the Login.jsp page
  2. complete user title and password
  3. click login button

Using the valid password, I'd enter into the restricted assets-seem not so difficult the issue is I've been trying to get this done using a simple java application using Apache's HttpCommons 3.1 package (HttpClient, GetMethod, PostMethod etc.) and I've been getting 302-resource moved in the server.

Here's my source code:

String strURL = "http://host:port/application/login/LoginForm.jsp"

        HttpState initialState = new HttpState()

        HttpClient httpclient = new HttpClient()




        GetMethod httpget = new GetMethod(strURL)

        int result = httpclient.executeMethod(httpget)

        System.out.println("Response status code: " + result)

        Cookie[] snacks = httpclient.getState().getCookies()

        String res1 = httpget.getResponseBodyAsString()


        PostMethod postMethod = new PostMethod("http://host:port/application/j_security_check")

        NameValuePair[] postData = new NameValuePair[2]

        postData[] = new NameValuePair("j_username", "username")

        postData[1] = new NameValuePair("j_password", "password")



        for (int i =  i < snacks.length i++)






        catch (HttpException httpe)


        catch (IOException ioe)


        String res2 = postMethod.getResponseBodyAsString()




    catch (IOException ex)


I'd attempted exactly the same kind of a good example from the same server using PHP code (CURL) and that i get submitted towards the page I'd expect following a effective login.

Any idea why exactly the same isn't employed in Java?

Thanks, - MK

You have to call postMethod.setFollowRedirects(true). The 302 is really a redirect towards the publish login page.

Per the documentation online (http://hc.apache.org/httpclient-3.x/redirects.html), HttpClient does not handle certain redirects automatically since the authors feel they are best handled through the user. If you are searching at scraping internet sites for several content, I have used a library known as WebHarvest to positive results previously. You'll find it on sourceforge : http://web-harvest.sourceforge.internet/

It is possible that HttpCommons does not support 302, try HttpUnit.