user481610
user481610

Reputation: 3270

Getting HTTP POST to work when making own Java Server

I'm looking to try and make a java server that can accept GET and POST HTTP requests. Now I've managed to get the GET method to work. But I'm not managing to get the POST method to work. My server manages to read the Request Header but doesn't seem to read the body of the message. i.e what was posted. Here is the code:

int port = 1991;

        ServerSocket serverSocket = new ServerSocket(port);

        System.err.println("The Server is on and listening on port " + port);
        System.out.println(" ");

        while (true) 
        {
            Socket ClientSocketConnection = serverSocket.accept();
            System.err.println("We have established a connection with a client!");
            System.out.println(" ");

            BufferedReader ServerInput = new BufferedReader(new InputStreamReader(ClientSocketConnection.getInputStream()));
            DataOutputStream ServerOutput =new DataOutputStream(ClientSocketConnection.getOutputStream());

            String StringInput;
            int iCount = 0;
            int CountNull = 0;

            while ((StringInput = ServerInput.readLine()) != null) 
            {

                System.out.println(StringInput);
            }

Now I simply display everything that is sent through the socket. But for some reason I just dont get the requests message body and I know the Body is sent because in chrome I have this:

enter image description here

I'm not sure how to get that "Form Data". Any help would really be appreciated!!

UPDATE:

Here is the problem further narrowed down. From sends the HTTP request fine. With an HTTP POST method we have the request header a \r\n and then the message data. The problem is when my BufferedData variable ServerInput reads in the \r\n (empty line) it stops reading from the ServerInput. Anyway to fix this?

Upvotes: 0

Views: 137

Answers (3)

user481610
user481610

Reputation: 3270

As mentioned in the post the problem I had was that the server I made read the HTTP request header but some how never managed to read the post information being sent to the server via Google chrome.

Now an HTTP post request has the following structure:

request header
\r\n
post information

The reason for me not being able to read the post information was because of the .readLine() function! Once the function reads in a \r\n it assumes that is the end of the message and stops reading the post information being sent and hence the error. To fix this problem I had to use the .read() function instead of the .readLine(). The .read() function reads in every character from the HTTP request which included the post information

Upvotes: 0

Jonathan
Jonathan

Reputation: 1367

I'd highly recommend you take a look at Jetty. It's an embeddable http server which will abstract all of this away for you.

Upvotes: 0

Zied Hamdi
Zied Hamdi

Reputation: 2660

You need to read about the HTTP protocol. You could take a look at HttpServlet api for that.

The purpose of servlets is exactly passing from a socket to an Http protocol. Are you sure you want to do the job again?

Upvotes: 2

Related Questions