HTTP Protocol - Basics

HTTP Protocol - Basics

What is protocol: A protocol is a set of rules. While talking about software, a protocol is a set of rules used to communicate between applications on same or different machines.
Example: Below are some protocols:
  1. http
  2. https
  3. ftp
  4. smtp
  5. telnet
  6. tcp
  7. ip
Applications communicates between each other by means of messages. Messages are of two types:
  1. Request Message: The message one application send to other as a request.
  2. Response Message: The message received by the requester as a response from server.
Message: A message consists of two parts - 
  1. Header: Header consists of some key value pairs like 
    1. Content Type
    2. Authorization
    3. Method
  2. Body: It contains information's like form data, file etc
Response message contain many parameters added by server as like Server, ContentLength etc.
One important think in response message is status code. 
Status Code: Status codes are three digit integer number, represents the status of the request as follows:
Code - 1xx: Informational Message only.
Code - 2xx: Success of some findings
Code - 3xx: Redirects the client to another URL
Code - 4xx: Error on clients part
Code - 5xx: Error on server part

Http Protocol: Hyper Text Transfer Protocol is a specifications or set of rules. Implementation of protocols are called  either client/server. Hence implementation of http protocol are called http client or http server. Complete implementation of http protocol is called http server and partial implementation is called http client.
Http Client: When we send request to a web server then we need a http client to talk to server and send request message to him. We usually do this by web browsers.


Sometimes customers request to a server via web browsers and then that server may need data access or other helps from some other servers. So, here the question arises that is "How server can request to other server?" as it doesn't have any http client in it as like the browsers. 
Then can use Apache HTTP client, Ok HTTTP client and so many others provided by several vendors.

Here is a simple example that creates a custom http client to browse a given website using Apache 
HTTP client:

public class MyHttpClient {

//"http://www.tariqnotes.blogspot.com/"

  public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter an url to browse:");
String url = sc.nextLine();
 
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      System.out.println(new String(responseBody));

    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    }  
  }
}

Comments

  1. Short and sweet basic outlines. Great job bhai. keep it up.

    ReplyDelete

Post a Comment