2

I am not looking to do anything complicated: I'm trying to do a bare-bones as-simple-as-possible transmission from client to server.

I know the way to do that with HTTP clients/servers is to use POST.

I've been trying to get even just a simple POST request to work for 6-7 hours now and have gotten nowhere. So I figured it was time to stop trying to figure it out on my own and post a question here: What is the simplest way to transmit a value from an HTTP client to an HTTP server coded in Java using a POST request?

I think I understand how to send data from the client, but I can't find anywhere that explains how to receive it at the server.

This is what I used in my server program (worked through a tutorial) just to test with a GET request from the client (it worked):

    public static void main(String args[]) throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress(8000),0);
    server.createContext("/test", new testHandler());
    server.setExecutor(null);
    server.start();
}
static class testHandler implements HttpHandler {

    @Override
    public void handle(HttpExchange t) throws IOException {
        String test = "Hello World!";
        t.sendResponseHeaders(200,test.length());
        OutputStream stream = t.getResponseBody();
        stream.write(test.getBytes());
        stream.close();
    }

How would I modify the above code to accommodate a POST request? (i.e. accept a value from the client).

user83676
  • 339
  • 1
  • 9
  • 15
  • I'm confused. You speak about sending a request from a client, yet now you post code about some embedded server. I would expect you to post code of the client application that you have been tinkering with for 6-7 hours. Confusion #2: if simple is what you are after, why on earth are you trying to roll your own? Use Jetty as the embedded server and use Apache HttpClient to send the request. Hundreds if not thousands of available examples will be at your disposal. – Gimby Oct 21 '15 at 07:18
  • Assuming that HttpServer up and running on required port, can you try this code? http://stackoverflow.com/questions/32779226/send-post-and-read-streaming-response/32779732#32779732 – Ravindra babu Oct 21 '15 at 07:18
  • Gimby - I had considered posting the client program as well, but I am relatively confident I know how it works and that I did it right. The server is where I don't know how to proceed. Regarding #2, I just thought it would be quicker/easier to whip something up myself without having to import/download a bunch of stuff - would that in fact be easier? @ravindra: That code is very similar to what I have in my client program. I'm using an output stream to send data over the connection. I just don't know how to accept it on the server side - how I would go about writing for that. – user83676 Oct 21 '15 at 07:29
  • I did not have exposure to HttpServer. But my code snippet works well for any kind of http urls. – Ravindra babu Oct 21 '15 at 11:55
  • Have a look at this question: http://stackoverflow.com/questions/3732109/simple-http-server-in-java-using-only-java-se-api – Ravindra babu Oct 21 '15 at 12:01

1 Answers1

1

I figured it out!

All I had to do was use a BufferedReader on the input stream of my HttpExchange object (since I was sending a value over an output stream in my client POST request).

I just added this to the server code I mentioned above:

BufferedReader input = new BufferedReader(new InputStreamReader(t.getRequestBody()));
        int a = input.read();
        String test = "You sent the value "+a+" to the server";

This message prints perfectly back to my client program now.

user83676
  • 339
  • 1
  • 9
  • 15