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.