1

I try to understand the principle of data exchange between Java and PHP.

I have this java source:

public static void main(String[] args) throws MalformedURLException, IOException {

    URL url;
    url = new URL ("http://www.corrosio.cz/text.php");

    PrintStream ps;
    BufferedReader reader;

    URLConnection conn;

    try {
        conn = url.openConnection();
        conn.setDoOutput(true);
        ps = new PrintStream(conn.getOutputStream());
        ps.print("firstKey=value1");
        ps.print("&secondKey=value2");

        conn.getInputStream();
        ps.close();                   //this section of java code works correctly

        System.out.print("Odeslané hodnoty:\t");     /* here I try to read the sent data,
                                                     but this part doesn't work. */
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = reader.readLine()) != null) {
            System.out.println(inputLine);

        }


    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }


}

This Java code works well, but only up to the level of ps.close() without the following section with BufferedReader.

Can someone explain and advise why I can't read the data sent from Java to PHP and then read the sent data using BufferedReader?

Here is the php script:

<?php
     foreach ($_POST as $key => $value) {
         switch ($key) {
                 case 'firstKey':
                        $firstKey = $value;
                        break;
                 case 'secondKey':
                        $secondKey = $value;
                        break;
        default:
                break;
         }
     }
     $mess = "hodnoty: " .$firstKey. " a " . $secondKey;

     mail("example@email.com", "Java", $mess);
     echo $mess;
?>

The mail() function works correctly, it sends an email with my Java data to my php email. Where is mistake? Thank You very much.

  • 1
    Can you try to use `$_GET` instead of `$_POST`? Also `$_REQUEST` should be possible. Anyway it looks to me that your java code runs a GET query. (not an java expert) Alternative: Enforce a POST request in your java code. – Christoph Kluge Mar 01 '20 at 17:58
  • @Christoph Kluge I'm a beginner in Java. can you show me a short example of how to using Java send a POST request? –  Mar 03 '20 at 15:06

2 Answers2

1

url.openStream() opens a new connection. To use the connection you already have, use conn.getInputStream() instead

Joni
  • 108,737
  • 14
  • 143
  • 193
1

According to the discussion inside the comments, here is my answer:

It looks to me that your java code runs a GET request instead of a POST request. (I'm not an java expert).

Can you try to use $_GET instead of $_POST inside your PHP code? Also $_REQUEST should be possible, which has the benefit that it gives you the result of $_GET, $_POST and $_COOKIES together.

https://www.php.net/manual/de/reserved.variables.request.php

A different alternative might be to enforce a POST request inside your java code. Here is a reference to another SO question which solves it.

https://stackoverflow.com/a/3325065/12880865

Please let me know if this helps or if you have any other questions.

Christoph Kluge
  • 1,947
  • 8
  • 23
  • Thank you for the useful information. Your efforts helped me get the key information. @Christoph Kluge –  Mar 04 '20 at 06:54