Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cookies with Java?

Tags:

java

cookies

How can I get a cookie from a web page using Java? I mean only Java not with Servlets or etc..

like image 720
cwarc Avatar asked May 07 '11 12:05

cwarc


People also ask

How do I enable cookies in Java?

Android. Open Google Chrome. From the web browser menu in the top-right corner, select Settings > Site settings > Cookies. From the Cookies menu, toggle the button on the right to Allow sites to save and read cookie data (recommended).

How do you get cookies in spring boot?

To set a cookie in Spring Boot, we can use HttpServletResponse class's method addCookie() . All you need to do is to create a new instance of Cookie class and add it to the response.

Which method is used to add a cookie?

The addCookie() method, for example, is used to add cookies to the response object. It then sends cookie data from a client to a server or server to a client using the HTTP response. The getCookies() method, on the other hand, is used to access the cookies that have been added to the response object.


1 Answers

You can use java.net.URLConnection for this. It offers a getHeaderFields() method to get the response headers. The cookies are set by Set-Cookie header.

URLConnection connection = new URL("http://google.com").openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...
like image 153
BalusC Avatar answered Oct 06 '22 00:10

BalusC