Wednesday, October 30, 2013

Login to or logout from Drupal in Java

I wanted to post and update a lot of biographical data to Drupal, but I could only do so by first logging in – manually. How tedious. So I thought that it would a good general tool to be able to programmatically login and logout of Drupal without having to go through the GUI each time. But how to do it?

The method I settled on was to use Java's HttpURLConnection class. I would open a http connection to Drupal, send it my username and password, get back a cookie and then send my stuff. When I had finished I could then programmatically logoff. Cool. But it turned out to be a bit more difficult than I had imagined. i used POST because I couldn't make GET work properly, and all applications seemed to use POST. Some of the parameters are probably not necessary, but I added them all from the packets I captured in Wireshark, and at least this way it works:

No comments:

Post a Comment