Teraz jeszcze takie pytanie.
mam takie żądanie w module logowania
używam oczywiście HttpURLConnection do połączenia.
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
Powinienem dostać cookie w odpowiedzi:
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, 19 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
A dostaje cos takiego:
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
<script>document.location.href='example.com'</script>
Mam wrazenie ze po prostu podczas połączenia ten obiekt dynamicznie przerzuca się na wskazaną lokalizację ale nie ustawia cookie. (W dodatku na stronie na prawdę się loguję, bo jak w przeglądarce jestem zalogowany, to mnie wylogowywuj, co oznacza, że kod prawie zadziałał i dla mnie stworzyła się nowa sesja a stara wygasła)
tak wyglada moj kod:
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)
{
}
Czy dobrze mi się wydaje?
Jak zrobić, żeby cookie się zapamiętywało i żebym się logował, jak w ogóle się dostać do niego?