hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mathias Söderberg" <mathias.soederb...@gmail.com>
Subject Another Form-problem, cookies?
Date Wed, 28 Nov 2007 21:45:59 GMT
Hi!
I, like many others, are trying to login to a website using HttpClient and
the PostMethod. When running the program I'm getting the following result:

Login form get: HTTP/1.1 200 OK
Initial set of cookies:
- PHPSESSID=4c3499e030f2e09c35e5af6fb20571f0
- checksum=f2d870271e0887c854fd041ad746cb78
Login form post: HTTP/1.1 200 OK
Logon cookies:
- PHPSESSID=4eb20a61a4a09f4cf89db46af25affb2
- checksum=0a8a452f089ad78ce3b0ea670d0d8011

And since I'm not very familiar with either php or cookies I really don't
know what to do next. I actually expected to get some kind of redirect or
something, but now I don't get anything like that, so basically I need some
help on what to do next.
I'm not really sure i got the NameValuePairs correct either...

Here's the page source from the website for the form;

<form method="post" action="takelogin.php">
<p>Note: You need cookies enabled to log in.</p>
<table border="0" cellpadding=5>
<tr><td class=rowhead>Username:</td><td align=left><input type="text"
size=40 name="username" /></td></tr>
<tr><td class=rowhead>Password:</td><td align=left><input
type="password" size=40 name="password" /></td></tr>
<!--<tr><td class=rowhead>Duration:</td><td align=left><input
type=checkbox name=logout value='yes' checked>Log me out after 15
minutes inactivity</td></tr>-->
<tr><td colspan="2" align="center"><input name="login" type="submit"
value="Log in!" class=btn></td></tr>
</table>
<input type="hidden" name="returnto" value="/" />
</form>


Any help is highly appreciated. Please guide me, I am in need of help.

Thanks
  Mathias


[CODE]
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.*;
import java.io.*;
import java.util.*;

/**
 * <p>
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 * </p>
 *
 * @author Oleg Kalnichevski
 *
 */
public class FormBasedTest {

    static final String LOGON_SITE = "www.torrentbytes.net";
    static final int LOGON_PORT = 80;

    public FormBasedTest() {
        super();
    }

    public static void main(String[] args) throws Exception {

        System.setProperty("org.apache.commons.logging.Log", "
org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("
org.apache.commons.logging.simplelog.showdatetime", "true");
        System.setProperty("
org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");
        System.setProperty("
org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
"debug");

        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,
"http");
        client.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
        // 'developer.java.sun.com' has cookie compliance problems
        // Their session cookie's domain attribute is in violation of the
RFC2109
        // We have to resort to using compatibility cookie policy

        GetMethod authget = new GetMethod("/login.php");

        client.executeMethod(authget);

        System.out.println("Login form get: " + authget.getStatusLine
().toString());
        // release any connection resources used by the method
        authget.releaseConnection();
        // See if we got any cookies
        CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
        Cookie[] initcookies = cookiespec.match(
                LOGON_SITE, LOGON_PORT, "/", false, client.getState
().getCookies());
        System.out.println("Initial set of cookies:");
        if (initcookies.length == 0) {
            System.out.println("None");
        } else {
            for (int i = 0; i < initcookies.length; i++) {
                System.out.println("- " + initcookies[i].toString());
            }
        }

        PostMethod authpost = new PostMethod("/login.php");
        // Prepare login parameters
        NameValuePair action = new NameValuePair("action", "takelogin.php");
        NameValuePair userid = new NameValuePair("username", "username");
        NameValuePair password = new NameValuePair("password", "password");
        NameValuePair login = new NameValuePair("login", "Log in!");
        NameValuePair hidden = new NameValuePair("returnto", "/browse.php");
        authpost.setRequestBody(
                new NameValuePair[]{action, userid, password, login,
hidden});

        client.executeMethod(authpost);
        System.out.println("Login form post: " + authpost.getStatusLine
().toString());
        // release any connection resources used by the method
        authpost.releaseConnection();

        // See if we got any cookies
        // The only way of telling whether logon succeeded is
        // by finding a session cookie
        Cookie[] logoncookies = cookiespec.match(
                LOGON_SITE, LOGON_PORT, "/", 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());
            }

        }

        // Usually a successful form-based login results in a redicrect to
        // another url
        int statuscode = authpost.getStatusCode();

        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
                (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
                (statuscode == HttpStatus.SC_SEE_OTHER) ||
                (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

            Header header = authpost.getResponseHeader("location");

            if (header != null) {

                String newuri = header.getValue();

                if ((newuri == null) || (newuri.equals(""))) {
                    newuri = "/";
                }

                System.out.println("Redirect target: " + newuri);
                GetMethod redirect = new GetMethod(newuri);

                client.executeMethod(redirect);
                System.out.println("Redirect: " +
                        redirect.getStatusLine().toString());

                // release any connection resources used by the method
                redirect.releaseConnection();

            } else {

                System.out.println("Invalid redirect");
                System.exit(1);

            }
        }
    }
}
[/CODE]

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