Cookies in java?
I was trying to login in to an online text game using java, I'm pretty sure the login part works.
What I think is the problem is that java doesn't store cookies so it returns to the login screen everytime I do something. Could anyone give me some advice on how to do this? Do I have to go through an internet browser? Or what?
[342 byte] By [
Bogada] at [2007-11-27 7:18:21]

What would the value of a cookie be? Does it look like this?
username
name
www.lostpower.com/game/
1600
2328136192
29936648
310864992
29863223
*
password
pass
www.lostpower.com/game/
1600
2328136192
29936648
310884992
29863223
*
save
true
www.lostpower.com/game/
1600
2328136192
29936648
310904992
29863223
*
Wow, do alot of forums/games store your username/passwords in clear text in cookies? Because this cookie did. But do I just put this in as the value of the cookie?
And... that didn't work, I put....
connection.addRequestProperty("Cookie", "usernamenamewww.lostpower.com/game/160023281361922993664831086499229863223*passwordpasswww.lostpower.com/game/160023281361922993664831088499229863223*savetruewww.lostpower.com/game/160023281361922993664831090499229863223*");
Bogada at 2007-7-12 19:06:01 >

> What would the value of a cookie be? Does it look
> like this?
>
> username
> name
> www.lostpower.com/game/
> 1600
> 2328136192
> 29936648
> 310864992
> 29863223
> *
[etc]
No, that's not a cookie, that's how a particular browser chooses to represent cookies in a data file.
That's not really the same thing. For example, your money is not your bankbook. Des Moines, Iowa is not the same thing as a map of Des Moines. Etc.
> Wow, do alot of forums/games store your
> username/passwords in clear text in cookies? Because
> this cookie did. But do I just put this in as the
> value of the cookie?
The forum/game, or the website in general, does not get to choose how the client stores the cookie. That's the browser's decision. Yes, arguably storing cookies in cleartext is a bad idea, even if the cookie isn't marked as secure.
> And... that didn't work, I put....
>
> connection.addRequestProperty("Cookie",
> "usernamenamewww.lostpower.com/game/160023281361922993
> 664831086499229863223*passwordpasswww.lostpower.com/ga
> me/160023281361922993664831088499229863223*savetruewww
> .lostpower.com/game/1600232813619229936648310904992298
> 63223*");
Most of that junk isn't part of a valid cookie header.
When a server sets a cookie on the client, it sends along some explicit metadata about the cookie, such as its expiration. Additionally, there is some implicit metadata about the cookie (such as what domain it was set on). So most of what you saw in the file was actually metadata, and the client doesn't send the metadata back to the server. The metadata is generally used to decide whether to send a cookie.
As I recall, setting the cookie on a given connection is more like this:
connection.addRequestProperty("Cookie", "name=Bob");
where "name" is the name of the cookie and "Bob" is the value of the cookie. However it's been a while since I had to do this.
The Apache Jakarta project has a subproject "HttpClient" which apparently handles cookies well.
Also in JDK 1.5, there's a new class java.net.CookieHandler, which as far as I can tell (I haven't used it) is meant to handle all that metadata. It looks kind of weird though...for some reason it's supposed to be system-wide? (What can be the advantage of that? What if the user wants to open multiple sessions with the same host?) Actually, a lot of the java.net stuff is set up system-wide (like basing proxies on system properties) which always struck me as a really weird decision.
Wow, that's pretty lucky, I just copied some random code, and made a call to it that I have no idea what it is. But it works! lol
But what you were talking about how you would regularly do it...
connection.addRequestProperty("Cookie", "name=Bob");
Would you just do that for all of the post properties? Or how would you know what the cookie needed to contain?
Bogada at 2007-7-12 19:06:01 >

I'm not certain what your question is.
You need to send back the cookies that the server sent to you. Well, you don't have to, but the server will make decisions based on the cookies that you send it, and it expects that you'll send it the cookies it told you to send.
It looks like CookieHandler should deal with the drudgery of looking at the cookies being sent by the server, storing them, and then telling you what cookies to send back to the server. It's not clear to me if there's an easy way to set the cookies, but you can always do it in a loop. There's probably an easier way though; try googling for tutorials.
Or you could just use HttpClient.