Hi all

Making a httpPost method which sends a form-login to a page. The page
responds with a 302. The documentation states that the httpClient should
automatically follow the redirect.

The problem is that i need to* call the metod twice* to get it to login ?

I have a servlet with a login method and a method for listing stuff.
First call, I don't get the information that requires login, but if I call
the servlet again, it works? Why?

my login code:

 HttpPost httpost = new HttpPost(url);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("btn_login"
, "xxx"));
        nvps.add(new BasicNameValuePair("txt_login", "xxx"));
        nvps.add(new BasicNameValuePair("psw_password", "xx"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


        response = httpClient.execute(httpost);


        System.out.println(response.getStatusLine().getStatusCode());

        String redirectLocation = null;

        for (Header h : response.getAllHeaders()) {
            if (h.getName().equals("Location")) {
                redirectLocation = h.getValue();
            }
        }

        HttpGet redirectGet = new HttpGet(redirectLocation );
          response =  httpClient.execute(redirectGet);
        System.out.println(response.getStatusLine().getStatusCode());

Do I need to make the redirect myself? Why doesnt it login on the first try?


Thanks

Reply via email to