views:

21

answers:

1

I'm having quite some trouble logging in to any site in Java. I'm using the default URLconnection POST request, but I'm unsure how to handle the cookies properly. I tried this guide: http://www.hccp.org/java-net-cookie-how-to.html But couldn't get it working. I've been trying basically for days now, and I really need help if anyone wants to help me.

I'll probably be told that it's messy and that I should use a custom library meant for this stuff. I tried downloading one, but wasn't sure how to get it set up and working. I've been trying various things for hours now, and it just won't work. I'd rather do this with a standard URLconnection, but if anyone can help me get another library working that's better for this, that would be great, too.

I would really appreciate if someone could post a working source that I could study. What I need is: POST login data to site -> Get and store the cookie from the site -> use cookie with next URLconnection requests to get logged-in version of the site.

Can anyone help me with this? Would be EXTREMELY appreciated. It really does mean a lot. If anyone wants to actually help me out live, please leave an instant-messenger address. Thank you a lot for your time.

A: 

This code works for me:

Getting the login-form (in my case, the cookie is actually set when I retrieve the login-page)

    URL url = new URL(formPage);
    URLConnection conn = url.openConnection();

    String cookieHeader = conn.getHeaderFields().get("Set-Cookie").get(0);
    Matcher m = JSESSION_PATTERN.matcher(cookieHeader);
    m.find();
    jSessionId = m.group(1);

I also need to grab a csrf value as well, but I'll omit that now.

I then login by doing:

    URL url = new URL(formPage);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestProperty("Cookie", "JSESSIONID=" + encode(jSessionId, "UTF-8")); 

    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.writeBytes(String.format("_csrf_token=%s&xyz=%s&zyx=%s",
            encode(csrfToken, "UTF-8"),
            encode(USER, "UTF-8"),
            encode(PASS, "UTF-8")));

    out.close();

    // I have absolutely no idea why this is needed.
    conn.getInputStream().close();


    String cookieHeader = conn.getHeaderFields().get("Set-Cookie").get(0);
    Matcher m = JSESSION_PATTERN.matcher(cookieHeader);
    m.find();
    jSessionId = m.group(1);

I can then retrieve and process pages using

    URL url = new URL(thePage);
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("Cookie", "JSESSIONID=" + encode(jSessionId, "UTF-8")); 

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // process str...

Hope that helps!

aioobe