Setting a http Cookie



Introduction

Http cookies are basically name/value pairs which can be saved to a Webbrowser to “remember” what the client (user) did. It helps to overcome the disadvantages of the stateless http protocol.

This example shows how to create a cookie and store it in the URLConnection property.

try {
    // Create a URLConnection object for a URL
    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    // Set the cookie value to send
    conn.setRequestProperty("Cookie", "name1=value1; name2=value2");

    // Send the request to the server
    conn.connect();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.