hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Blomqvist <blomqvist.andr...@gmail.com>
Subject login problems with post and redirect.
Date Thu, 18 Feb 2010 13:49:12 GMT
Hi

Doing a POST login to a site (which I dont control) and manually
redirecting. However I am not getting logged in until I make a second call
to the login method. Why?

My code for login :

 private HttpContext login(DefaultHttpClient httpClient, String button,
String user, String pass) throws UnsupportedEncodingException, IOException {

        HttpResponse response = null;
        HttpEntity entity = null;
        HttpPost httpost = new HttpPost(BASE_URL +
"/index.php?func=do_login");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("btn_login", button));
        nvps.add(new BasicNameValuePair("txt_login", user));
        nvps.add(new BasicNameValuePair("psw_password", pass));

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

        HttpContext context = new BasicHttpContext();
        response = httpClient.execute(httpost, context);

        System.out.println("Post status is " + response.getStatusLine());


        entity = response.getEntity();
        if (entity != null) {
            entity.consumeContent();
        }

        HttpUriRequest request = (HttpUriRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);

        String uri = BASE_URL + request.getURI().toString();

        System.out.println("uri in login " + uri);

        HttpGet redirect = new HttpGet(uri);
        response = httpClient.execute(redirect, context);

        System.out.println("Redirect HttpGet status is " +
response.getStatusLine());


        entity = response.getEntity();
        if (entity != null) {
            entity.consumeContent();
        }

        return context;
    }

After a call to this method I get output: (checking login status is method I
made to check for att logout link on the page.

  [java] Post status is HTTP/1.1 302
     [java] uri in login url_to_first_page
     [java] Redirect HttpGet status is HTTP/1.1 200
     [java] Checking login statuts: false


Any thoughts?

/A

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message