Creating cookies in Java

clansfear
9 years ago | edited 9 years ago

0

Hello,
I wanted to solve coding levels with Java. The problem is that when I connect with HttpsUrlConnection class I get “guest” response from the web as I’m missing cookies. I tried to set them with setRequestProperty method but with no luck, I copied all my cookies but I guess PHPSESSID is actually the only that matters. How can I easily set the cookies correctly?
My code:

```import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Networking101 {

public static void main(String[] args) throws Exception {  

    URL strona = new URL("https://www.hackthis.co.uk");  
    HttpsURLConnection sslstrona = (HttpsURLConnection)strona.openConnection();  
    sslstrona.setRequestProperty("Cookie", "PHPSESSID=SOMETHING :)");  
    sslstrona.connect();  

    BufferedReader czytasz = new BufferedReader(new InputStreamReader(sslstrona.getInputStream()));  

    String zawartosc;  

    while ((zawartosc=czytasz.readLine())!=null)    {  
        System.out.println(zawartosc);  
    }  
    czytasz.close();  

}  

}```

5replies
2voices
213views
dloser
9 years ago

0

My Java knowledge is a bit limited, but I think it should work. However, if you are running the code on a computer with a different IP address, it will not.

Other options are to have the code log in to HT itself or use the ‘autologin’ cookie (which requires the user-agent to be the same as when you got it).

clansfear
9 years ago

0

Thanks, you got me somewhere with the autologin, I set my browser to fake Java client and logged in and added the cookie to the program… now I have another problem. Connection stops due to an infinite redirection loop, I suspect I will have to greatly expand my cookie handling to stop it.
What I probably need is a CookieManager and CookieHandler, but the documentation seems somewhat vague to me as I’m just starting with Java Networking. It would be great if someone could give me an example.

dloser
9 years ago

0

Try python or something. Much much easier there. :)

In Java, it think you can stop the redirect with set(Instance)FollowRedirects(). This way you can get the session from the first request and use that in a second one.

With a bit of searching you should be able to get information on these kind of questions. I did one search and immediately got something that shows how you can use the cookie classes.

clansfear
9 years ago | edited 9 years ago

0

Thanks, I decided to use Java specifically as I wanted to learn networking in it for a long time and it was a nice opportunity. What is more i managed to fix it by changing the order in which the cookies are set. I set the autologin first, and after it the phpsessid. If only I could understand why it works now :P

dloser
9 years ago

0

Getting it to work doesn’t really mean it is fixed. ;)

Could be that session is now linked to the right address. You can’t set multiple cookies with multiple calls to setRequestProperty().

You must be logged in to reply to this discussion. Login
1 of 6

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss