W3C home > Mailing lists > Public > www-jigsaw@w3.org > May to June 2004

request for clientside post+cookie example (sorry if it hits the list twice)

From: Harmen <harmenj@zonnet.nl>
Date: Sat, 12 Jun 2004 17:10:14 +0200
Message-ID: <008001c4508f$5d02b360$4c744651@geesloo>
To: <www-jigsaw@w3.org>

working on a java interface for a html site I have a lot questions about cookie handling.
There are a lot snippets of sourcecode posted on the mailing list, but they are old (of 2000 and earlier) and/or just not working.

Can someone please give me an example about how the cookies are correct handled? (the way it is ment?)

thanks in advance!

Harmen Jeurink

The way i do it now: (ugly!) (just to illustrate)

   userName = tfUserName.getText();
   password = tfPassword.getText();
   String redirectURL = login(userName, password);

private String login(String userId, String password){ 
// login on site 
// and returns redirect URL, sets global String cookie with cookie data

  Request request = client.createRequest();
  HttpSetCookieList cookieList1, cookieList2;
  Reply reply;

  String postdata = "uid="+ userId + "& pwd="+password; // post data

  try {
   request.setURL(new URL(HOST + LOGON_SITE));
   MimeType mt = new MimeType("application/" + "x-www-form-urlencoded");
   reply = client.runRequest(request);
   HeaderValue cookieValue = reply.getHeaderValue("set-cookie"); // retrieve value of the cookie set after successfull login
   HeaderValue redirectURLHeader = reply.getHeaderValue("location:"); // retrieve redirect url 
    cookieList1 = (HttpSetCookieList) cookieValue.getValue(); 
    cookieList2 = (HttpSetCookieList) redirectURLHeader.getValue(); // here it starts to get ugly...

    cookie=cookieList1.toString(); // cookie value into global string, since we cannot return more than 1 parameter in java
    return cookieList2.toString();  // return redirect URLHeader
  catch bloks{}
  return ("IOException or HttpException or MimeTypeFormatException");

private void displayHTML(String redirectURL) {
  Request request = client.createRequest();
  Reply reply;
  request.setUserAgent(USER_AGENT + "\r\nCookie: "+cookie); // add cookie in http Header, and even more uglier
  try {
    request.setURL(new URL(redirectURL));
    MimeType mt = new MimeType("application/" + "x-www-form-urlencoded");
    reply = client.runRequest(request);
    // do something with the reply
Received on Saturday, 12 June 2004 11:10:16 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 22:41:31 UTC