How to send cookies to the server? [Java Code]

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL. Click Send to execute Send Cookies example online and see the results. The Java code was automatically generated for the Send Cookies example.
How to send cookies to the server? [Java Code] Send
GET /echo HTTP/1.1
Cookie: name=value; name2=value2

Updated: Viewed: 23652 times
Java code for Send Cookies example

Java code for Send Cookies Example

This Java code snippet was generated automatically for the Send Cookies example.
<< Back to the Send Cookies example

What is HTTP?

The Hypertext Transfer Protocol is the data transfer protocol that underlies the World Wide Web. HTTP transfers data between HTTP clients (browsers and mobile apps) and servers. HTTP is based on a client-server architecture. HTTP works as a request-response protocol between client and server. The client initiates a connection and sends a request. The server receives the request, executes it, and sends the result to the client.

What is Cookie?

HTTP Сookies are small pieces of text data sent by a website and stored on a user's computer by a web browser. The data stored in the cookie is created by the server when it processes a request from the client. This data is tagged with an identifier that is unique to you and your computer. The browser sends cookies back to the server with each subsequent request. The server reads the identifier from the cookie and determines whether the request came from the same browser or not. After that, the server knows exactly what information can be provided to the user (based on user permissions). Cookies are mainly used to manage user sessions, track users, store user preferences and site settings.

Cookies Syntax
Cookie: [cookie-name=value]

Why use Cookies?

Cookies are mainly used for three purposes:

  • Session management: Logins, passwords, or whatever the server needs to remember
  • Personalization: Custom settings, themes, and other settings
  • Tracking: Recording and analysis of user behavior

How to see saved Cookies in the browser?

To see saved Cookies in your browser:

  1. Navigate to a website;
  2. Press F12 to open the developer tool;
  3. Click the Application tab;
  4. In the tree in the Storage section, select Cookies and your website address;
  5. Here, you will see all the Cookies stored for your browser for this site.

How to send Cookies?

An example of sending Cookies header to the ReqBin echo URL:

Send Cookies Example
GET /echo HTTP/1.1
Cookie: name=value; name2=value2

See also

Generate code snippets for Java and other programming languages

Convert your Send Cookies request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the Java code generator.