In the current world, internet is became a very big media i.e.; net users are growing day by day. Most popular protocol that will be used by internet applications is HTTP. Here I'm going to explain you about this in depth.
As you know a protocol is a set of rules that needs to be followed by two computers while communicating each other on the network.
HTTP protocol rules
1. Client has to establish the connection with the server.
2. Server has to wait for client's request and ready to serve for client's request.
3. On receiving the client request, server has to process the client request and send the response to client.
4. Client has to receive the response.
Any server application which follows the above rules, called as web server. Examples: Tomcat, Weblogic, Microsoft IIS etc.
Any client application which follows the above rules, called as web client or user-agent or browser. Examples: Internet explorer, Mozilla Firefox, and Google chrome, etc.
In the above paragraph, we've discussed about the request and response. Now our next question is what is request? and what is response?
A request is a data that will be sent by the client to perform an action from the server. Simply, its a collection of bytes which will be sent to the server to establish the connection.
HTTP is a standard and as per the HTTP specification, HTTP request should follow a structure.
HTTP request structure
Each HTTP request has multiple lines, where each line should be separated by a carriage return followed by new line (\r\n). First line called, Initial Request Line, contains the HTTP method followed by the resource address (URL) followed by HTTP version.
Eg: GET http://java-servlet-jsp-web.blogspot.com HTTP/1.0
Followed by the initial request, a blank line should be there and then followed by request headers should be placed. A request header is a key value pair separated by a colon (:). Multiple request headers should be separated by a new line (\r\n). Each header will represent a piece of information that helps server to process the request.
Example request headers:
USER-AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/22.214.171.124
And all the request headers should be followed by a blank line and then followed by HTTP request body.
So a programatically, if you want to send a HTTP request, you should follow the above steps.
GET http://java-servlet-jsp-web.blogspot.com HTTP/1.0\r\n\r\nUser-agent: My browser\r\n\r\n"
The above example is sending a request to the resource www.java-servlet-jsp-web.blogspot.com with the request header User-agent (Normally each browser will send this header with the browser name here it is My browser) and blank request body.
As we discussed, like client follows some rules to construct an HTTP request, server has to construct the HTTP response as well, which also has a standard.
HTTP response structure
HTTP response also is a combination of lines. Its sequence is similar to the request, starts with Initial response line followed by blank line, response headers, blank line, HTTP response body.
Initial response line will have the HTTP version, status code, and status description.
Eg: HTTP/1.0 200 OK (or) HTTP/1.0 404 Not Found
Each HTTP status code represent a predefined status, follow up my blog for more info on HTTP status codes.
Similar request headers, response headers are also key value pairs, which will be separated by colon (:). And this time server is sending piece of information to the client.
Response body will have the HTML content of the page, the source that you can see at the browser end (Clicking on view page source), will be placed as part of the response body.
Hope you are very clear on HTTP protocol now. I'll post more info on HTTP status codes, HTTP headers and some client, server programs using Java.
Click here to checkout more about HTTP methods.