Search code examples
javahttpgethttpclienthead

http server and client implementing head and get method


I'm trying to make a simple http client server using java. It will show the client's request as well as the server's response. For example server will send back

   HTTP/1.0 200 OK
   Connection: Close. etc.

Previously i had a echo client server. Now I've turned my echo server to act as a http server. I've tried Goggling about how to implement the head and get with the client but i noticed usually all of the example used apache framework. Is there a way to implement these method without apache framework. My echo client which i'm trying to convert into a http client:

       import java.io.*;
       import java.net.*;

          public class Ec1
      {
      public static void main(String[] args)
        {
           try
        {
          Socket s = new Socket("127.0.0.1", 80);
        BufferedReader r = new BufferedReader(new                                 InputStreamReader(s.getInputStream()));
            PrintWriter w = new PrintWriter(s.getOutputStream(), true);
           BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
          String line;
           do
        {
         line = r.readLine();
          if ( line != null )
        System.out.println(line);
       line = con.readLine();
          w.println(line);
        }
         while ( !line.trim().equals("bye") );
          }
          catch (Exception err)
        {
         System.err.println(err);
        }
        }
     }

My Http server:

          import java.io.BufferedReader;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.net.ServerSocket;
          import java.net.Socket;

          public class Echo
                 { 

 protected void start() {
        ServerSocket s;

        System.out.println("Webserver starting up on port 80");

        try {
          // create the main server socket
          s = new ServerSocket(80);
        } catch (Exception e) {
          System.out.println("Error: " + e);
          return;
        }
        Socket clientSocket = null; 
        System.out.println ("Waiting for connection.....");
        try { 
             clientSocket = s.accept(); 
             System.out.println("Connection, sending data.");
             BufferedReader in = new BufferedReader(new InputStreamReader(
                     clientSocket.getInputStream()));
             PrintWriter out = new PrintWriter(clientSocket.getOutputStream());


             String str = ".";
             while (!str.equals(""))
               str = in.readLine();


             out.println("HTTP/1.0 200 OK");
             out.println("Content-Type: text/html");
             out.println("Server: Bot");

             out.println("");

             out.println("<H1>Welcome to the Ultra Mini-WebServer</H2>");
             out.flush();
             clientSocket.close();
             s.close(); 
           } catch (Exception e) {
             System.out.println("Error: " + e);
           }
         }

        public static void main(String args[]) {
            WebServer ws = new WebServer();
            ws.start();
                    }
           }

Solution

  • Yes, there is, just interpret your request you're getting from the client.

    From the following code (in HttpServer), parse:

    String str = ".";
    while (!str.equals("")) {
      str = in.readLine();
    
      if (str.startsWith("HEAD")) {
          //Head execution here...
      }
    }
    

    Etc...