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,applicat… 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 <script>document.location.href='exampl… 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-Agen… "Opera/9.80 (Windows NT 6.0; U; cs) Presto/2.5.22 Version/10.50"); connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,applica… connection.setRequestProperty("Accept-La… "pl,en-us;q=0.7,en;q=0.3"); connection.setRequestProperty("Accept-En… "gzip,deflate"); connection.setRequestProperty("Accept-Ch… "ISO-8859-2,utf-8;q=0.7,*;q=0.7"); connection.setRequestProperty("Keep-Aliv… connection.setRequestProperty("Connectio… keep-alive"); connection.setRequestProperty("Referer" , "example.com"); connection.setRequestProperty("Content-T… "application/x-www-form-urlencoded"); connection.setReadTimeout(10000); connection.setRequestProperty("Content-L… Integer.toString(content.getBytes().leng… 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?
-
Answer:
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("… ...);
Kaszynek at Yahoo! Answers Visit the source
Related Q & A:
- How To Enable Cookies?Best solution by Yahoo! Answers
- How to clear the cookies?Best solution by Yahoo! Answers
- How to submit a form using httpurlconnection?Best solution by stackoverflow.com
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.