Carrying an Http Session

Hello,

My friend has a website with registeration system, he asked me to make a java client that asks the same questions and such on site, I was succesful in sending POSTs and reading response, I carried on through the session by resubmitting the HiddenFields, but once I finish the last step the server generates an error, always, maybe I'm not handling cookies very well, please tell me everything I need to supply to carry on a succesful session, also tell me if there's an error in the way I handle cookies:

Note: There's some global variables used in here

public String post(String urlR, String data)

{

String out="";

try{

//sending the first POST request

URL url =new URL(urlR);

URLConnection conn = url.openConnection();

conn.setDoOutput(true);

if(pageCookie != 1)

{

conn.setRequestProperty("Cookie",cookieToBeSent);

conn.connect();

}

else

{

conn.connect();

}

OutputStreamWriter wr =new OutputStreamWriter(conn.getOutputStream());

wr.write(data);

wr.flush();

// Get the response

BufferedReader rd =new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;

while ((line = rd.readLine()) !=null)

{

out += line+"\n";

}

wr.close();

rd.close();

cookie ="";

String headerName="";

int cookieCount = 0;

for (int i=1; (headerName = conn.getHeaderFieldKey(i))!=null; i++)

{

if (headerName.equalsIgnoreCase("Set-Cookie"))

{

cookie = conn.getHeaderField(i);

cookieCount++;

StringTokenizer st =new StringTokenizer(cookie,";=");

if(pageCookie == 1 && cookieCount > 1)

{

cookieToBeSent += st.nextToken() +"=";

cookieToBeSent += st.nextToken() +";";

}

}

}

pageCookie++;

}catch (Exception e){}

return out;

}

I've noticed that the server sends cookies just the first time, the other times it just resends the same cookie over and over, so I handeld it that way, but I'm not sure if I'm sending cookies correctly, also the first cookie has a name but no value, and the 2nd cookie has the same name and with value, so I didn't read the 1st one.

[3734 byte] By [YaseenTTa] at [2007-10-2 0:27:49]
# 1
I would advise you to look at HttpCLietn from apache. you can find it under jakarta. It's a good lib.Marteijn
Maerteijna at 2007-7-15 16:42:14 > top of Java-index,Administration Tools,Sun Connection...