hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Veena Sarolkar" <sarolkar_ve...@extenprise.com>
Subject RE: HTTPS redirection with form posting
Date Tue, 01 Mar 2005 13:46:27 GMT
Hi,

I have read the complete documentation of HttpClient.
I am using it as an application not a web application so can't send the log.
I am just sending the code I tried...


public class PostTry {

  /**
   * Constructor for BasicAuthenticatonExample.
   */
  public PostTry() {
    super();
  }

  public static void main(String[] args) throws Exception {
    System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    // create a GET method that reads a file over HTTPS, we're assuming
    // that this file requires basic authentication using the realm above.
    PostMethod post = new PostMethod("https://gna.org/account/login.php");


    post.setDoAuthentication( true );
    post.setFollowRedirects(false) ;

    NameValuePair form_loginname= new NameValuePair("form_loginname",
"veena");
    NameValuePair form_pw= new NameValuePair("form_pw", "veena");
    NameValuePair stay_in_ssl= new NameValuePair("stay_in_ssl", "1");
    NameValuePair cookie_for_a_year= new NameValuePair("cookie_for_a_year",
"0");
    NameValuePair login= new NameValuePair("login", "login");
    post.setRequestBody(
        new NameValuePair[] {form_loginname, form_pw, stay_in_ssl,
cookie_for_a_year,login});

    client.executeMethod(post);
    System.out.println("Login form post: " +
post.getStatusLine().toString());

    post.releaseConnection();
    // See if we got any cookies
    // The only way of telling whether logon succeeded is
    // by finding a session cookie
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(
        "gna.org", 443, "/", false, client.getState().getCookies());
    System.out.println("Logon cookies:");
    if (logoncookies.length == 0) {
      System.out.println("None");
    }
    else
    {
      for (int i = 0; i < logoncookies.length; i++) {

        System.out.println("- " + logoncookies[i].toString());
      }
    }

    int statuscode = post.getStatusCode();
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
        (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
        (statuscode == HttpStatus.SC_SEE_OTHER) ||
        (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT))
    {
      Header header = post.getResponseHeader("location");
      post.getResponseBodyAsString();

      post.releaseConnection();
      if (header != null)
      {
        String newuri = header.getValue();
        if ((newuri == null) || (newuri.equals("")))
        {
          newuri = "/";
        }
        System.out.println("Redirect target: " + newuri);
        GetMethod redirect = new GetMethod(newuri);

        logoncookies = cookiespec.match(
            "gna.org", 443, "/my", false, client.getState().getCookies());

        System.out.println("Logon cookies:");
        if (logoncookies.length == 0) {
          System.out.println("None");
        }
        else
        {
          for (int i = 0; i < logoncookies.length; i++) {

            System.out.println("- " + logoncookies[i].toString());
          }
        }

        client.executeMethod(redirect);

        System.out.println("response :"+redirect.getStatusLine());
        System.out.println("response :"+redirect.getResponseBodyAsString());

        // release any connection resources used by the method
        redirect.releaseConnection();
      }
      else
      {
        System.out.println("Invalid redirect");
        System.exit(1);
      }
    }
  }
}

It is giving me proper set of cookies for original as well as redirected
urls.
But it is not giving the page next to login but still giving the original
page in the response.
Am I missing something?

Veena



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message