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).

5
  • 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. Commented Oct 21, 2015 at 7:18
  • Assuming that HttpServer up and running on required port, can you try this code? stackoverflow.com/questions/32779226/… Commented Oct 21, 2015 at 7: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. Commented Oct 21, 2015 at 7:29
  • I did not have exposure to HttpServer. But my code snippet works well for any kind of http urls. Commented Oct 21, 2015 at 11:55
  • Have a look at this question: stackoverflow.com/questions/3732109/… Commented Oct 21, 2015 at 12:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's great. Just update the answer with 1) Code before fix 2) Code after fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.