• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java: Handling cookies when logging in with POST  RSS feed

 
Cris Carter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: HOW-TO: Handling Cookies Using the java.net API 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. Sorry that my first post has to be one asking for help, but I really do need it. And yes, I've tried googling for hours, nothing's working out. As I said, any help would be so awesome.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use the Apache HttpClient? It does all that behind the scenes. There are several examples.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that question is too difficult for us just "beginning" Java, so I shall move this thread.
 
Cris Carter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the Apache HTTPClient thing. The problem is that all the tutorials for logging in to sites and handling cookies with it are for an older version of it, and they don't work, at least not with what I'm downloading right now. Plus, I'm not sure how to actually add the library to my Java project. I'm using IntelliJ. Do you have any useful links or tips that could help me out? Thanks a lot for the answer, by the way.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right-click on project, "Module Settings". There are "Project Settings" and "Platform Settings". Under "Project Settings" there's a "Libraries"; clickie. Either add the library to an existing library set or create a new one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!