Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Handeling session cookies with Java

Posted on 22/12/2009

Let’s say you are creating an HTTP request using Java.

You are probably doing it with the use of the URL object like this:

			URL u = new URL("http://www.google.com/");
			URLConnection uc = u.openConnection();
			uc.connect();

But sometimes you will also want to use cookies to send along with you request.

To do so first we will learn how to read cookies from the request.

Getting the cookies from a URL connection
After the function uc.connect(); you are able to get cookies from your request.

			URL u = new URL("http://www.google.com/");
			URLConnection uc = u.openConnection();
			uc.connect();

			String headerName=null;
			for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) {
			 	if (headerName.equals("Set-Cookie")) {                  
				String cookie = uc.getHeaderField(i);  
				System.out.println(cookie);
			 	}
			 }

Here is the result of this example:

PREF=ID=f0fd8b4c16931772:TM=1261387922:LM=1261387922:S=LLhLkHSpgfXkqxIe; expires=Wed, 21-Dec-2011 09:32:02 GMT; path=/; domain=.google.co.uk
NID=30=C6Hb5SV2-pqYymnnrZmTAprCvDMrFwo6wzg9We7rvPztXmA_fTWxv-GPmDVXPsIBOOkecj1Ms4skmTWT-uUN5iW_-ZYiPUNNJXE8CC44xWVORe8Yyu1jI32XL51tvzCz; expires=Tue, 22-Jun-2010 09:32:02 GMT; path=/; domain=.google.co.uk; HttpOnly

Sending HTTP request with cookies:

			URL u = new URL("http://www.google.com/");
			URLConnection uc = u.openConnection();
			uc.setRequestProperty("Cookie", "name1=value1; name2=value2");
			uc.connect();

Notice that the value of the property ‘Cookie’ is a string which contains cookies in the format of cookieName=CookieValue with a ‘;’ as a delimiter.

Notice you have to set the cookie value before the connect() function.

downloaddownload source

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme