0

I'm trying to create a client server application and I'm currently stuck. I have this java code on my client.

        HttpURLConnection connection;
        connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        connection.setRequestProperty("Accept","application/json: charset=UTF-8");
        connection.setRequestMethod("POST");

        System.out.println("The request method on client end is " + connection.getRequestMethod());
        System.out.println("Server response to connection " + connection.getResponseMessage());

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String buffer;
        StringBuilder stringBuilder = new StringBuilder();
        while ((buffer = reader.readLine()) != null) {
            stringBuilder.append(buffer);
        }
        System.out.println("The request methond on server end is " + stringBuilder.toString()); 

And this is the simple test php code on the server.

  <?php   echo $_SERVER['REQUEST_METHOD'];  ?>

Whenever I run this test java program and try to connect I get the same output.

The request method on client end is POST
Server response to connection OK
The request methond on server end is GET

The php script always echoes back that I'm sending a GET request even though my java code states that use a POST. I have tried connecting to the script through postman using different request methods and it's all fine, so the problem must be in the Java code. Any insight would be greatly appreciated.

apboutos
  • 109
  • 2
  • 16
  • Please take a look at the answer to https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily. They seems to be having a similar issue. – Magdrop Sep 06 '18 at 04:45

1 Answers1

0

I am not having any experience of php but it seems like it is looking for content-length header which will not be send in your case so it is treating this as GET request.

HttpURLConnection connection;
         connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
         connection.setDoOutput(true);
         connection.setDoInput(true);
         connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
         connection.setRequestProperty("Accept","application/json: charset=UTF-8");
         connection.setRequestMethod("POST");
         connection.setFixedLengthStreamingMode(0);
         System.out.println("The request method on client end is " + connection.getRequestMethod());
         System.out.println("Server response to connection " + connection.getResponseMessage());

         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
         String buffer;
         StringBuilder stringBuilder = new StringBuilder();
         while ((buffer = reader.readLine()) != null) {
             stringBuilder.append(buffer);
         }
         System.out.println("The request methond on server end is " + stringBuilder.toString()); 

This will set content length 0 as you are not sending any content.

vinay chhabra
  • 587
  • 3
  • 7
  • I tried it is working now it giving exception as it has been moved tempoarily.Just check my edited answer as per your code – vinay chhabra Sep 06 '18 at 05:13