views:

53

answers:

2

As part of my dissertation thesis I have constructed a "bot" that crawls the official forum of the course Programming in C and searches for frequently asked questions in order to post a reply. I am at the part that I must simulate the login in order to be able to post the appropriate reply. The login form is as follows:

!-- login form -- form action="login.php?do=login" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)"> script type="text/javascript" src="clientscript/vbulletin_md5.js?v=385"> table cellpadding="0" cellspacing="3" border="0"> tr> td class="smallfont" style="white-space: nowrap;">Όνομα χρήστη td>

td class="smallfont" nowrap="nowrap">Αυτόματη Σύνδεση /tr> tr> td class="smallfont">Κωδικός td> td> /tr> /table>

input type="hidden" name="s" value="" /> input type="hidden" name="securitytoken" value="guest" /> input type="hidden" name="do" value="login" /> input type="hidden" name="vb_login_md5password" /> input type="hidden" name="vb_login_md5password_utf" /> /form> !-- / login form -->

I have come to understand that i need to md5 hash the password but I cannot login whatsoever. I use the post method and i prepare the content doing the following:

content = "do=login&url=login.php" + "&vb_login_md5password=" + md5_pass+ "&vb_login_md5password_utf="+ md5_pass + "&s=&securitytoken=guest&vb_login_username=" + UserName + "&vb_login_password=" + PassWprd;

I then send the contend by doing the following:

urlConnection = (HttpURLConnection)(new URL(targetUrl).openConnection());

        // Specifying that we intend to use this connection for input
        urlConnection.setDoInput(true);

        // Specifying that we intend to use this connection for output
        urlConnection.setDoOutput(true);

        // Specifying the method of HTTP request which is POST
        // throws ProtocolException
        urlConnection.setRequestMethod("POST");

        // Specifying the content type of our post
        urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);
        urlConnection.setRequestProperty("Content-length",String.valueOf (content.length()));
       // urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");

        // Prepare an output stream for writing data to the HTTP connection
        // throws IOException
        dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
        dataOutputStream.writeBytes(content);

Nonetheless, I cannot login. I believe that I have something wrong at the form that I send but I cannot find what. Any help would be appreciated as I have to finish the program in a few days.

A: 

Instead of trying to forge HTTP requests by hand, I would instead suggest you to rely upon a virtual web client, like HtmlUnit. it allows you to go at a higher level and, instead of trying to forge a HTTP query with the right elements in, you "simply" have to fill the form with the correct values.

Riduidel
A: 

I think best would be if you first try to see wether your http-request is equal to the one created by the browser at normal login.

To do so, you could install the Live HTTP Header addon in Firefox (https://addons.mozilla.org/de/firefox/addon/3829/) - this will allow you to see, how exactly the browsers login request looks like.

On the java side, the toString() method is overwritten and should display the complete header, afaik. If not, just compare the content string and make sure, that you did set all of the right headers.

Alex
I did what you said and I found the right form and the right POST login link and finally it worked. Thank you for your help :P
fysob