With the development of technology, we now live in a world where the internet gives us the freedom to use a variety of applications, including internet surfing, email, file transfer, video and audio streaming, and much more. Yet, none of these things are possible without client-server communication using specific protocols like HTTP, SMTP, FTP, POP, and others.
One of the most extensively used protocols on the internet is HTTP, or HyperText Transfer Protocol. You will know everything there is to know about HTTP and how it functions by the time you finish reading this article.
The following are some essential HTTP concepts you need to know first:
- Since HTTP is an asymmetric request-response client-server protocol, the client sends a request message to the server, which then sends the client the response message.
- Because it is a stateless protocol, the request being processed at the moment is unaware of anything that has occurred in the past.
Have you ever pondered what occurs when you enter Google.com into your web browser? Well, your web browser converts your requested URL into a request message and sends it to the HTTP server. Following that, the HTTP server will evaluate the requested message and return a response or error message back to your web browser.
For instance, the browser will convert the URL www.google.com to something like this:
GET /docs/index.html HTTP/1.1
HOST: www.google.com
Accept: image/gif, image/jpeg, */*
Accept-language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Now, the server has two options when it receives this request message:
i. Analyzes the received request message, converts it into a file in the server’s document directory, and then sends the request file back to the client.
ii. Analyzes the received request message, maps it to a server program, runs it, and finally sends the results back to the client.
iii. It sends the client an error message if the request cannot be fulfilled.
Protocol for the Network Layer
There must be a set of established protocols between machines, such as between the client and the server, in order for them to easily communicate with one another across the internet. TCP and UDP Protocol are the two sets of protocols in charge of establishing communication between two machines.
TCP — Transmission Control Protocol is a type of protocol that is dependable, acknowledgement is required, and each packet includes a sequence number. A packet will definitely be sent again if the receiver doesn’t receive it the first time. Unlike UDP, the protocol ensures the delivery of packets. When downloading files, browsing the web, and other situations are used.
UDP — User Datagram Package Although it has minimal network overhead, UDP does not ensure packet delivery. It’s widely used in applications where reliability is not crucial such as gaming, streaming video and audio, and other similar ones.
Often used with TCP/IP connections, HTTP is a client-server application protocol. The IP (Internet Protocol), which comes in IPv4 and IPv6 versions, is a network-layer protocol that deals with network addressing and routing. Although the HTTP protocol’s default port number is 80, you are free to use alternative user-defined port numbers like 8000 or 8080 instead.
Request and Response Message for HTTP
Text messages are sent over the internet during HTTP client and server communication. The server receives a request message from the client and responds with a response message.
— HTTP REQUEST MESSAGE
The request line and request headers that make up an HTTP request message are separated by a blank line.
— — Request Line
The syntax for the request line is:
request-method-name request-URI HTTP-version
- request-method-name: HTTP protocols specify a number of request methods, including GET, POST, HEAD, and OPTIONS. One of these approaches may be used by the client to make a request to the server.
- request-URI: The requested resource is specified by the request-URI.HTTP
- version: Indicates whether the request is for HTTP/1.0 or HTTP/1.1.
Example of request-line are:
GET /test.html HTTP/1.1
HEAD /query.html HTTP/1.0
POST /index.html HTTP/1.1
— — Request Headers
Name:value pairs make up the request headers. Commas are used to separate several values.
The request header has the following syntax:
request-header-name: request-header-value1, request-header-value2, …
Example of request-headers are:
Host: www.google.com
Connection: Keep-Alive
Accept: image/gif, image/jpeg, */*
Accept-Language: us-en, fr, cn
The status line and response headers of an HTTP response message are separated by a blank line.
— — Status Line
The status line appears on the first line, and then a response header is optional (s). The status line’s syntax is as follows:
HTTP-version status-code reason-phrase
Example of status line are:
HTTP/1.1 200 OK
HTTP/1.0 404 Not Found
HTTP/1.1 403 Forbidden
— — Response Headers
Name:value pairs make up the response headers. Response headers are formatted as follows:
response-header-name: response-header-value1, response-header-value2, …
Example of response headers are:
Content-Type: text/html
Content-Length: 35
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
The HTTP REQUEST METHODS
The request methods are defined by the HTTP protocol. One of these request methods may be used by a client to communicate a request to an HTTP server. The methods are:
GET: A client can make a GET request to the server to obtain a web resource.
HEAD: A client can utilize the HEAD request to acquire the header that would have been obtained through a GET request. As the header includes the data’s most recent modification date, it is possible to compare it to the local cache copy.
POST: A method for uploading data to a web server.
PUT: Request that the server save the data.
DELETE: Request that the server remove the data.
TRACE: Request a diagnostic trace of the server’s operations.
OPTIONS: Request a list of the request methods that the server accepts.
CONNECT: This command instructs a proxy to connect to another host and send the requested content without parsing or caching it. To establish an SSL connection over a proxy, this is frequently utilized.
— The HTTP Request Methods Examples
a. “GET” Request Method
GET request-URI HTTP-version
E.g
GET /index.html HTTP/1.0
Note: You can test HTTP requests by sending raw request messages to an HTTP server using programs like “telnet” or “hyperterm” or by writing your own network application.
Assuming that the HTTP server is operating on localhost (IP address 127.0.0.1) at port 8000, the following Java program example is shown:
import java.net.*;
import java.io.*;
public class HttpClient {
public static void main(String[] args) throws IOException {
// The host and port to be connected.
String host = "127.0.0.1";
int port = 8000;
// Create a TCP socket and connect to the host:port.
Socket socket = new Socket(host, port);
// Create the input and output streams for the network socket.
BufferedReader in
= new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out
= new PrintWriter(socket.getOutputStream(), true);
// Send request to the HTTP server.
out.println("GET /index.html HTTP/1.0");
out.println(); // blank line separating header & body
out.flush();
// Read the response and display on console.
String line;
// readLine() returns null if server close the network socket.
while((line = in.readLine()) != null) {
System.out.println(line);
}
// Close the I/O streams.
in.close();
out.close();
}
}
b. “HEAD” Request Method
HEAD request is similar to GET request. However, the server returns only the response header without the response body, which contains the actual document. HEAD request is useful for checking the headers, such as Last-Modified, Content-Type, Content-Length, before sending a proper GET request to retrieve the document.
The syntax of the HEAD request is as follows:
HEAD request-URI HTTP-version
(other optional request headers)
(blank line)
(optional request body)
Example:
HEAD /index.html HTTP/1.0
(blank line)
c. “OPTIONS” Request Method
To find out the request methods the server supports, a client can utilize the OPTIONS request method. The OPTIONS request message syntax is as follows:
OPTIONS request-URI|* HTTP-version
(other optional headers)
(blank line)
Example:
OPTIONS http://www.amazon.com/ HTTP/1.1
Host: www.amazon.com
Connection: Close
(blank line)
d. “TRACE” Request Method
To request a diagnostic trace from the server, a client might make a TRACE request. The TRACE request message syntax is as follows:
TRACE / HTTP-version
(blank line)
Example:
TRACE http://www.amazon.com/ HTTP/1.1
Host: www.amazon.com
Connection: Close
(blank line)
e. “POST” Request Method
With the post request method, you can “post” extra data to the server, like HTML form submissions or file uploads. A GET request is always made when the browser issues an HTTP URL. You can use an HTML form with the attribute method=”post” or create your own network application to start a POST request.
The post request takes the following syntax:
POST request-URI HTTP-version
Content-Type: mime-type
Content-Length: number-of-bytes
(other optional request headers)
(URL-encoded query string)
Example 1: Submitting Form Data using POST Request Method
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>LOGIN</h2>
<form method="post" action="/bin/login">
Username: <input type="text" name="user" size="25" /><br />
Password: <input type="password" name="pw" size="10" /><br />
<input type="hidden" name="action" value="login" />
<input type="submit" value="SEND" />
</form>
</body>
</html>
Example 2: File Upload using multipart/form-data POST Request
<html>
<head><title>File Upload</title></head>
<body>
<h2>Upload File</h2>
<form method="post" enctype="multipart/form-data" action="servlet/UploadServlet">
Who are you: <input type="text" name="username" /><br />
Choose the file to upload:
<input type="file" name="fileID" /><br />
<input type="submit" value="SEND" />
</form>
</body>
</html>
I hope this article has been helpful to you. Don’t forget to give me a clap 👏 and follow me on twitter and facebook.