Ich habe ein Problem, wenn ich eine PHP-Seite über J2ME (Java) mit der POST-Methode aufrufe. (Die GET-Methode funktioniert ohne Probleme)
PHP-Code:
<?php
echo "var=".$_POST["var"];
?>
Java-Code:
String url="http://meineSeite.bplaced.net/meinePHPDatei.php";
String query="var=test";
HttpConnection hc = null;
ByteArrayOutputStream baos = null;
InputStream is = null;
try {
byte[] body = query.getBytes();
//REQUEST
hc = (HttpConnection) Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Connection", "close");
hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length", Integer.toString(body.length));
OutputStream out = hc.openOutputStream();
out.write(body);
out.flush();
out.close();
//RESPONSE
int i;
is = hc.openInputStream();
baos = new ByteArrayOutputStream();
while ((i = is.read()) != -1) {
baos.write(i);
}
String responseMessage = baos.toString();
System.out.println("Response: " + responseMessage);
} catch (Exception e) {
} finally {
try {
if (baos != null) {
baos.close();
}
if (is != null) {
is.close();
}
if (hc != null) {
hc.close();
}
} catch (Exception e) {
}
}
}
Als Antwort erhalte ich immer:
Response: var=
Hat jemand eine Ahnung warum die POST-Methode nicht funtkioniert?
Edit
Ach ja, wenn ich die PHP-Seite lokal hoste, funktioniert auch die POST-Methode