java-networking

HTTP

The HyperText Transfer Protocol (HTTP) is a standard that defines how a web client talks to a web server and how data is transferred from the server back to the client. It is the foundation of any data exchange on the Web.

The HTTP Protocol

HTTP is the standard protocol used to transfer data over the web. It is a request-response protocol that works as follows:

  1. A client sends a request to a server.
  2. The server processes the request and sends a response back to the client.
  3. The client processes the response.
  4. The connection is closed.

HTTP is a stateless protocol, meaning that each request-response pair is independent of any other request-response pair. This means that the server does not store any information about the client between requests.

HTTP is a connectionless protocol, meaning after the client has established a connection with a server, sent a request, and received a response, the connection is immediately dropped.

HTTP Connections use the TCP/IP protocol for data transfer. The default port for HTTP is 80.

A typical client request:

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

A typical server response

HTTP/1.1 200 OK
Date: Mon, 26 Aug 2024 12:00:00 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Sun, 25 Aug 2024 23:11:55 GMT
ETag: "1a3b-603fcbf0"
Accept-Ranges: bytes
Content-Length: 6715
Keep-Alive: timeout=5, max=1000
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example Page</title>
</head>
<body>
    <h1>Welcome to Example.com</h1>
    <!-- Rest of the HTML content -->
</body>
</html>

HTTP Methods

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. These methods are also known as HTTP verbs. Each method has a specific purpose and defines how the server should interact with the resource.

Syntax :

METHOD /path/to/resource HTTP/1.1
Date: Mon, 26 Aug 2024 12:00:00 GMT
Host: www.example.com
Content-Type: application/json
Content-Length: 25

{
  "key": "value"
}


1. GET

Retrieve data from a specified resource.

Characteristics:

2. POST

Submit data to be processed to a specified resource.

Characteristics:

name=John&age=30


#### 3. **PUT**
Update or replace a resource with new data.

**Characteristics:**
- If the resource does not exist, it can create a new one.
- The request body contains the new data.
- Idempotent (repeating the same PUT request will result in the same outcome).

**Example**

PUT /users/123 HTTP/1.1 Host: www.example.com Content-Type: application/json

{ “name”: “John Doe”, “age”: 30 }


#### 4. **DELETE**
Delete the specified resource.

**Characteristics:**
- Removes the resource from the server.
- Idempotent (repeating the same DELETE request will result in the same outcome).

**Example**

DELETE /users/123 HTTP/1.1 Host: www.example.com



#### 5. **HEAD**
Retrieve the headers of a resource without the body.

**Characteristics:**
- Similar to GET, but the response contains only headers, no body.
- Useful for checking if a resource exists or if it has been modified.

**Example**

HEAD /index.html HTTP/1.1 Host: www.example.com



#### 6. **PATCH**
Apply partial modifications to a resource.

**Characteristics:**
- Updates only certain parts of the resource.
- Not necessarily idempotent, depending on the nature of the changes.

**Example**

PATCH /users/123 HTTP/1.1 Host: www.example.com Content-Type: application/json

{ “age”: 31 }



#### 7. **OPTIONS**
Describe the communication options for the target resource.

**Characteristics:**
- Used to determine the allowed HTTP methods on a resource.
- The response contains the allowed methods in the Allow header.

**Example**

OPTIONS /index.html HTTP/1.1 Host: www.example.com



#### 8. **CONNECT**
Establish a tunnel to the server, often used for HTTPS.

**Characteristics:**
- Commonly used with SSL/TLS for encrypted communication.

**Example**

CONNECT www.example.com:443 HTTP/1.1 Host: www.example.com


## Status Codes

- 1xx: Informational
- 2xx: Success
- 3xx: Redirection
- 4xx: Client Error
- 5xx: Server Error

![Status codes](https://restfulapi.net/wp-content/uploads/HTTP-Error-Codes.jpg)

# HTTP Cookies

HTTP Cookies are small pieces of data sent by a server to a client's web browser and stored on the client side. They are used to remember information about the user across multiple requests, such as authentication status, user preferences, and tracking information.


### 1. **Cookie Structure**
Cookies consist of key-value pairs, along with optional attributes that define their scope and behavior.

**Example:**
``` http
Set-Cookie: sessionId=abc123; Expires=Wed, 27 Sep 2024 12:00:00 GMT; Path=/; Domain=example.com; Secure; HttpOnly

CookieHandler is an abstract class in Java that provides a mechanism for managing cookies.

CookieManageris the default implementation of CookieHandler and manages cookies using a CookieStore.

A Custom Cookie Manager allows developers to implement their own cookie management logic by extending CookieManager or CookieHandler.

CookieStore is an interface used by CookieManager to store cookies. It defines how cookies are stored and retrieved.

Methods:

Default Implementation: The default CookieStore implementation stores cookies in memory.

CookieStore store = cookieManager.getCookieStore();

Custom Implementation: You can implement your own CookieStore to store cookies in a database, file, or any other storage mechanism.

Example:

  CookieStore cookieStore = new InMemoryCookieStore();
  CookieManager cookieManager = new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
  CookieHandler.setDefault(cookieManager);

HttpCookie

The HttpCookie class in Java represents an HTTP cookie, allowing you to manage cookies in HTTP communication. It’s part of the java.net package.

Key Features:

Constructor

HttpCookie(String name, String value)

Creates a cookie with the specified name and value.

Common Methods

Example

HttpCookie cookie = new HttpCookie("username", "Prem");
cookie.setDomain("example.com");
cookie.setPath("/");
cookie.setMaxAge(3600); // 1 hour

This example creates a cookie with a 1-hour lifespan, valid for the “example.com” domain.

Program :

CookieManager cookieManager = new CookieManager();
CookieStore store = cookieManager.getCookieStore();

// creating cookie and uri
HttpCookie c1 = new HttpCookie("user1", "1");
HttpCookie c2 = new HttpCookie("user2", "2");

URI uri1 = URI.create("htttp://spm.com.np");
URI uri2 = URI.create("htttp://spm.com.np");


// add
store.add(uri1, c1);
store.add(uri2, c2);


List<HttpCookie> cookiesList = store.getCookies();

// remove
store.remove(uri1,c1);

//remove all
store.removeAll();