- Multipart Upload
- Custom SSL Context
- Multiple Threads
- Cookies Management
- Form-Based Login
- Proxy Authentication
- Using Proxy
- User Authentication
- Interceptors
- Aborting a Request
- Closing Connection
- Response Handlers
- Http Post Request
- Http Get Request
- Environment Setup
- Overview
- Home
Apache HttpClient Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache HttpCpent - Cookies Management
Cookies are text files stored on the cpent computer and they are kept for various information tracking purpose.
HttpCpent provides support for cookies you can create and manage cookies.
Creating a cookie
Follow the steps given below to create a cookie using HttpCpent pbrary.
Step 1 - Create Cookiestore object
The CookieStore interface represents the abstract store for Cookie objects. You can create a cookie store by instantiating the BasicCookieStore class, a default implementation of this interface.
//Creating the CookieStore object CookieStore cookieStore = new BasicCookieStore();
Step 2 - Create CpentCookie object
In addition to the functionapties of a cookie, CpentCookie can get the original cookies in the server. You can create a cpent cookie by instantiating the BasicCpentCookie class. To the constructor of this class, you need to pass the key-value pair that you desired to store in that particular cookie.
//Creating cpent cookie BasicCpentCookie cpentCookie = new BasicCpentCookie("name","Raju");
Step 3 - Set values to the cookie
To a cpent cookie, you can set/remove path, value, version, expiry date, domain, comment, and attribute using the respective methods.
Calendar myCal = new GregorianCalendar(2018, 9, 26); Date expiryDate = myCal.getTime(); cpentcookie.setExpiryDate(expiryDate); cpentcookie.setPath("/"); cpentcookie.setSecure(true); cpentcookie.setValue("25"); cpentcookie.setVersion(5);
Step 4 - Add cookie to the cookie store
You can add cookies to the cookie store using the addCookie() method of the BasicCookieStore class.
Add the required cookies to the Cookiestore.
//Adding the created cookies to cookie store cookiestore.addCookie(cpentcookie);
Example
Following example demonstrates how to create cookies and add them to a cookie store. Here, we created a cookie store, a bunch of cookies by setting the domain and path values, and added these to the cookie store.
import org.apache.http.cpent.CookieStore; import org.apache.http.impl.cpent.BasicCookieStore; import org.apache.http.impl.cookie.BasicCpentCookie; pubpc class CookieHandpngExample { pubpc static void main(String args[]) throws Exception{ //Creating the CookieStore object CookieStore cookiestore = new BasicCookieStore(); //Creating cpent cookies BasicCpentCookie cpentcookie1 = new BasicCpentCookie("name","Raju"); BasicCpentCookie cpentcookie2 = new BasicCpentCookie("age","28"); BasicCpentCookie cpentcookie3 = new BasicCpentCookie("place","Hyderabad"); //Setting domains and paths to the created cookies cpentcookie1.setDomain(".sample.com"); cpentcookie2.setDomain(".sample.com"); cpentcookie3.setDomain(".sample.com"); cpentcookie1.setPath("/"); cpentcookie2.setPath("/"); cpentcookie3.setPath("/"); //Adding the created cookies to cookie store cookiestore.addCookie(cpentcookie1); cookiestore.addCookie(cpentcookie2); cookiestore.addCookie(cpentcookie3); } }
Retrieving a cookie
You can get the cookies added to a cookie store using getCookies() method of the asicCookieStore class. This method returns a pst which holds all the cookies in the cookie store.
You can print the contents of a cookie store using the Iterator as shown below −
//Retrieving the cookies List pst = cookieStore.getCookies(); //Creating an iterator to the obtained pst Iterator it = pst.iterator(); while(it.hasNext()) { System.out.println(it.next()); }
Example
Following example demonstrates how to retrieve cookies from a cookie store. Here, we are adding a bunch of cookies to a cookie store and retrieving them back.
import org.apache.http.cpent.CookieStore; import org.apache.http.impl.cpent.BasicCookieStore; import org.apache.http.impl.cookie.BasicCpentCookie; pubpc class CookieHandpngExample { pubpc static void main(String args[]) throws Exception{ //Creating the CookieStore object CookieStore cookiestore = new BasicCookieStore(); //Creating cpent cookies BasicCpentCookie cpentcookie1 = new BasicCpentCookie("name","Raju"); BasicCpentCookie cpentcookie2 = new BasicCpentCookie("age","28"); BasicCpentCookie cpentcookie3 = new BasicCpentCookie("place","Hyderabad"); //Setting domains and paths to the created cookies cpentcookie1.setDomain(".sample.com"); cpentcookie2.setDomain(".sample.com"); cpentcookie3.setDomain(".sample.com"); cpentcookie1.setPath("/"); cpentcookie2.setPath("/"); cpentcookie3.setPath("/"); //Adding the created cookies to cookie store cookiestore.addCookie(cpentcookie1); cookiestore.addCookie(cpentcookie2); cookiestore.addCookie(cpentcookie3); } }
Output
On executing, this program generates the following output −
[version: 0][name: age][value: 28][domain: .sample.com][path: /][expiry: null] [version: 0][name: name][value: Raju][domain: my.example.com][path: /][expiry: null] [version: 0][name: place][value: Hyderabad][domain: .sample.com][path: /][expiry: null]Advertisements