- From: Harmen <harmenj@zonnet.nl>
- Date: Sat, 12 Jun 2004 17:10:14 +0200
- To: <www-jigsaw@w3.org>
- Message-ID: <008001c4508f$5d02b360$4c744651@geesloo>
Hello,
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!
cheers
Harmen Jeurink
The way i do it now: (ugly!) (just to illustrate)
actionPerformed{
userName = tfUserName.getText();
password = tfPassword.getText();
String redirectURL = login(userName, password);
displayHTML(redirectURL);
}
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;
request.setMethod("POST");
request.setUserAgent(USER_AGENT);
String postdata = "uid="+ userId + "& pwd="+password; // post data
request.setContentLength(postdata.length());
try {
request.setURL(new URL(HOST + LOGON_SITE));
MimeType mt = new MimeType("application/" + "x-www-form-urlencoded");
request.setContentType(mt);
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.setMethod("GET");
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");
request.setContentType(mt);
reply = client.runRequest(request);
// do something with the reply
}
}
Received on Saturday, 12 June 2004 11:10:16 UTC