Question by Kaszynek: Java – HttpURLConnection – cookies?
I want to login on the web.
I’m using HttpURLConnection to connect

POST /aaa.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://example.com/
Content-Type: application/x-www-form-urlencoded
Content-Length: 33
login=login123&password=password123

I should get cookie in response.

HTTP/1.1 302 Found
Date: Sat, 01 May 2010 14:01:32 GMT
Server: Apache
Set-Cookie: PHPSESSID=dsf34213aad3412sf12f6c; path=/
Set-Cookie: login=8login123password123 expires=Sun, 02-May-2010 14:01:32 GMT; path=/
Expires: Thu,9 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /go/location2/bbb.php
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Connection: close
Content-Type: text/html; charset=utf-8

This are headers wchich i get from my firefox when i’m logging into this site.

But In my programm i get:

null HTTP/1.1 200 OK
Date Sun, 02 May 2010 08:06:34 GMT
Server Apache
Vary Accept-Encoding
Content-Length 62
Connection close
Content-Type text/html; charset=utf-8

I think that this object dynamic is redirected to the location, but there isn’t set cookie. (Additional on the site I think that I’m logged in because my session from firefox end in the moment when I’m running my programme)

This is my code:

URL myurl = new URL(“example.com/aaa.php”);

try{
connection = (HttpURLConnection)myurl.openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“Host” , “example.com”);
connection.setRequestProperty(“User-Agent”, “Opera/9.80 (Windows NT 6.0; U; cs) Presto/2.5.22 Version/10.50″);
connection.setRequestProperty(“Accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8″);
connection.setRequestProperty(“Accept-Language”, “pl,en-us;q=0.7,en;q=0.3″);
connection.setRequestProperty(“Accept-Encoding”, “gzip,deflate”);
connection.setRequestProperty(“Accept-Charset”, “ISO-8859-2,utf-8;q=0.7,*;q=0.7″);
connection.setRequestProperty(“Keep-Alive”,”115″);
connection.setRequestProperty(“Connection”,” keep-alive”);
connection.setRequestProperty(“Referer” , “example.com”);
connection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
connection.setReadTimeout(10000);
connection.setRequestProperty(“Content-Length”, Integer.toString(content.getBytes().length));
connection.setDoInput(true);
connection.setDoOutput(true);

//Connection
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (content);
wr.flush ();
wr.close ();

InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append(‘\r’);
}
rd.close();

for (int i=0; ; i++)
{
String headerName = connection.getHeaderFieldKey(i);
String headerValue = connection.getHeaderField(i);

if (headerName == null && headerValue == null)
{
break;
}
else
System.out.println(headerName + ” ” + headerValue);
}

System.out.println(response.toString());
}
catch(Exception ex)
{

}

Did I have right?
How to make cookie got set and login into site?
I’know but i dont get “set-cookie”

Man, this is what i get from the ‘for’ loop

null HTTP/1.1 200 OK
Date Sun, 02 May 2010 08:06:34 GMT
Server Apache
Vary Accept-Encoding
Content-Length 62
Connection close
Content-Type text/html; charset=utf-8

but i should get

HTTP/1.1 302 Found
Date: Sat, 01 May 2010 14:01:32 GMT
Server: Apache
Set-Cookie: PHPSESSID=dsf34213aad3412sf12f6c; path=/
Set-Cookie: login=8login123password123 expires=Sun, 02-May-2010 14:01:32 GMT; path=/
Expires: Thu,9 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /go/location2/bbb.php
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Connection: close
Content-Type: text/html; charset=utf-8

Best answer:

Answer by Germann A
It look about right, do you see the output from
System.out.println(headerName + ” ” + headerValue);
?

If you do, than you will need to store these values in your program AND pass them back in your further requests
HttpURLConnection.setRequestProperty(“Cookie”, …);

Give your answer to this question below!

Be Sociable, Share!