A web client/http client/user agent communicates with the web server/http server using hyper text transfer protocol.

ClientServer

According to http protocol the client and server must follow the steps given below for communicating

  1. Client must establish a connection with the server.
  2. Client must send a request to the server.
  3. Server must process the request
  4. The server sends the response to the browser.
  5. The lines of http request and http response must be terminated using \r \n.
  6. According to http protocol the client must send the request using the format shown below
  7. HttpRequest

  8. The initial request line must contain 3 portions
  9. IntialRequestLine

  10. The header line is split into two portions
  11. headerline

import java.io.*;
import java.net.*;
public class WebCli{
  public static void main(String a[])throws Exception{
	String host="localhost";
	int port=8000;
	InetAddress ia = InetAddress.getByName(host);
	Socket sock = new Socket(ia,port);
	InputStream sis = sock.getInputStream();
	OutputStream sos = sock.getOutputStream();
	PrintStream ps = new PrintStream(sos);
	String req = "GET/one.html HTTP/1.0\r\n user-agent:our browser1.0\r\n\r\n";
	ps.print(req);
	System.in.read();
	System.in.read();
	byte[] bresp = new byte[2000];
	int nobr = sis.read(bresp,0,2000);
	String sresp = new String(bresp,0,nobr);
	System.out.println("--RESPONSE--");
	System.out.println(sresp);
  }
}

The format of response is similar to the format of request.

formateresponse

The initial response line is devided into three portions.

InitialResponseLine

  1. According to HTTP Protocol the server is responsible for passing the status codes as part of the response various status codes that can be used are
    • 1xx — used for passing information
    • 2xx — used for success
    • 3xx — used for redirection
    • 4xx — used for represent failure due to client
    • 5xx — used for represent failure due to server
  2. According to the HTTP protocol the server can support different request methods some of the request methods are GET, HEAD, POST, DELETE, OPTIONS, TRACE…etc.