0

Here is my java http server program:

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(); 
        } 
    } 
} 
}

While this program is running, if i write "http://localhost:8080" 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.

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:

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();
}
}

But i want to send HTTPGET request via TCP socket connection to my localhost. How can i do that?

Ozlu
  • 1,255
  • 1
  • 14
  • 20

1 Answers1

1

To do that, you have to print header of request. For a basic HTTP request, just add http method and host in header.

See the code bellow

Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("GET / HTTP/1.1");
pw.println("Host: stackoverflow.com");
pw.println("");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String t;
while ((t = br.readLine()) != null) {
    System.out.println(t);
}
br.close();

Good luck

thepaulo
  • 370
  • 4
  • 10
  • Actually, i already see that post:http://stackoverflow.com/questions/10673684/send-http-request-manually-via-socket ---But i can not understand a point that i can even write that: pw.print("HELLO\r\n"); pw.print("Host: AHMET\r\n\r\n"); pw.flush(); ---so i mean, i can print whatever i write, i guess i can not understand the header of request logic... – Ozlu Dec 11 '16 at 16:07
  • Do not use print and carriage return characters. Use println. Do like the answer on this post. – thepaulo Dec 11 '16 at 16:12
  • 1
    @Sonor: this code does not use proper line endings (i.e. `\r\n`) but most servers will ignore this. But the code issues a HTTP/1.1 request which means implicit keep-alive, i.e. the server might maybe not close directly after sending the response so the code might just hang there. Instead the client should properly deal with the response and check for Content-Length or chunked encoding in the HTTP header. Also, while it makes sense to read the header as lines it makes no sense to do this for the body. – Steffen Ullrich Dec 11 '16 at 17:49
  • First of all, i must to say that thank you so much! However, i want to send HTTPGET request to my localhost, so how can i do that? I am not sure that i can do it with Sonor's way! I already developed my http server with java as you can see in the above, all i just need that "send HTTPGET via tcp socket"... – Ozlu Dec 11 '16 at 19:35
  • See this link, it can hepl you: https://www.codeproject.com/tips/1040097/create-simple-http-server-in-java – thepaulo Dec 12 '16 at 09:10