title: Send HTTPGET request over TCP connection to "localhost"
body: <p>Here is my java http server program:</p>
  
  <pre><code>package alan;
  
  import java.io.BufferedReader; 
  import java.io.IOException; 
  import java.io.InputStreamReader; 
  import java.net.ServerSocket; 
  import java.net.Socket; 
  
  public class SimpleHTTPServer { 
      public static void main(String args[] ) throws IOException { 
          ServerSocket server = new ServerSocket(8080); 
          System.out.println("Listening for connection on port 8080 ...."); 
          while (true) { 
              Socket clientSocket = server.accept(); 
              InputStreamReader isr = new          InputStreamReader(clientSocket.getInputStream()); 
          BufferedReader reader = new BufferedReader(isr); 
          String line = reader.readLine(); 
          while (!line.isEmpty()) { 
              System.out.println(line); 
              line = reader.readLine(); 
          } 
      } 
  } 
  }
  </code></pre>
  
  <p>While this program is running, if i write "<a href="http://localhost:8080" rel="nofollow noreferrer">http://localhost:8080</a>" on the web browser, the program can handle the Http get request and it prints the result on the Eclipse console but I want to do it by using java code. </p>
  
  <p>Actually, first of all, i want to create a class which name is SimpleHTTPClient and i want to create a TCP Socket connection with SimpleHTTPServer class and send HTTPGET request via java code to my localhost. How can i do that? Actually, i can send HTTPGET request with URL connection like this:</p>
  
  <pre><code>package alan;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.Socket;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.UnknownHostException;
  
  public class SimpleHTTPClient {
      static Socket socket = null;
  
      public static void main(String args[]) throws UnknownHostException,     IOException {
      URL oracle = new URL("http://localhost:8080");
      URLConnection yc = oracle.openConnection();
      BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
      String inputLine;
      while ((inputLine = in.readLine()) != null) 
          System.out.println(inputLine);
      in.close();
  }
  }
  </code></pre>
  
  <p>But i want to send HTTPGET request via TCP socket connection to my <strong><em>localhost</em></strong>. How can i do that?</p>