Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a cookie and use it in HttpURLConnection?

I have the following python code which creates a cookie and adds it to the session. What would be the equivalent java code for it using HttpURLConnection? I basically want to do a HTTP POST request using the generated cookie.

    session = requests.session()
    session.auth = (username, password)
    try:
        token = session.get(SITEMINDER_URL % server, verify=False)
        session.cookies.update(dict(SMSESSION=json.loads(token.content)['SMSESSION']))
    except Exception as ex:
        raise Exception("Failed in authenticating with siteminder", ex)
    response = session.post(api_url, headers=headers, verify=False, json=data)
like image 314
Kaushik Lingerkar Avatar asked Nov 23 '25 08:11

Kaushik Lingerkar


1 Answers

Try This:

    URL url = new URL("http://www.example.com");
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    conn.setRequestProperty("Cookie", "name1=value1; name2=value2");

    conn.connect();
like image 66
Gaurav Shakya Avatar answered Nov 25 '25 20:11

Gaurav Shakya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!