Hi everybody.
After years of C# and PHP, I'm finally returning to Java.
My goal is to create a Java program capable of sending images to a PHP Photo Album on my web server.
Right now, however, I am stuck trying to send simple text variables through POST to my PHP script.
The code does seem to connect to the script like it is supposed to, but it seems unable to send the POST variables. The PHP script is returning a 'Undefined index' warning for the variables, like they don't exists.
My Java Code:
[code=java]
public static void main(String[] args) {
try {
URL url;
URLConnection urlConnection;
DataOutputStrea m outStream;
DataInputStream inStream;
// Build request body
String body =
"fName=" + URLEncoder.enco de("Atli", "UTF-8") +
"&lName=" + URLEncoder.enco de("Þór", "UTF-8");
// Create connection
url = new URL("http://192.168.1.68/test/POST/post.php");
urlConnection = url.openConnect ion();
((HttpURLConnec tion)urlConnect ion).setRequest Method("POST");
urlConnection.s etDoInput(true) ;
urlConnection.s etDoOutput(true );
urlConnection.s etUseCaches(fal se);
urlConnection.s etRequestProper ty("Content-Type", "applicatio n/x-www-form-urlencoded");
urlConnection.s etRequestProper ty("Content-Length", ""+ body.length());
// Create I/O streams
outStream = new DataOutputStrea m(urlConnection .getOutputStrea m());
inStream = new DataInputStream (urlConnection. getInputStream( ));
// Send request
outStream.write Bytes(body);
outStream.flush ();
outStream.close ();
// Get Response
// - For debugging purposes only!
String buffer;
while((buffer = inStream.readLi ne()) != null) {
System.out.prin tln(buffer);
}
// Close I/O streams
inStream.close( );
outStream.close ();
}
catch(Exception ex) {
System.out.prin tln("Exception cought:\n"+ ex.toString());
}
}
[/code]
And the PHP code:
[code=php]
<?php
# Read GET variables
$fName = $_POST['fName'];
$lName = $_POST['lName'];
# Create output string
$str = "\"$fName\" , \"$lName\"\n ";
# Open and write to file
$fh = fopen("postData .txt", "a+");
fwrite($fh, $str);
fclose($fh);
echo "done";
?>
[/code]
Any thoughts?
Thanks.
After years of C# and PHP, I'm finally returning to Java.
My goal is to create a Java program capable of sending images to a PHP Photo Album on my web server.
Right now, however, I am stuck trying to send simple text variables through POST to my PHP script.
The code does seem to connect to the script like it is supposed to, but it seems unable to send the POST variables. The PHP script is returning a 'Undefined index' warning for the variables, like they don't exists.
My Java Code:
[code=java]
public static void main(String[] args) {
try {
URL url;
URLConnection urlConnection;
DataOutputStrea m outStream;
DataInputStream inStream;
// Build request body
String body =
"fName=" + URLEncoder.enco de("Atli", "UTF-8") +
"&lName=" + URLEncoder.enco de("Þór", "UTF-8");
// Create connection
url = new URL("http://192.168.1.68/test/POST/post.php");
urlConnection = url.openConnect ion();
((HttpURLConnec tion)urlConnect ion).setRequest Method("POST");
urlConnection.s etDoInput(true) ;
urlConnection.s etDoOutput(true );
urlConnection.s etUseCaches(fal se);
urlConnection.s etRequestProper ty("Content-Type", "applicatio n/x-www-form-urlencoded");
urlConnection.s etRequestProper ty("Content-Length", ""+ body.length());
// Create I/O streams
outStream = new DataOutputStrea m(urlConnection .getOutputStrea m());
inStream = new DataInputStream (urlConnection. getInputStream( ));
// Send request
outStream.write Bytes(body);
outStream.flush ();
outStream.close ();
// Get Response
// - For debugging purposes only!
String buffer;
while((buffer = inStream.readLi ne()) != null) {
System.out.prin tln(buffer);
}
// Close I/O streams
inStream.close( );
outStream.close ();
}
catch(Exception ex) {
System.out.prin tln("Exception cought:\n"+ ex.toString());
}
}
[/code]
And the PHP code:
[code=php]
<?php
# Read GET variables
$fName = $_POST['fName'];
$lName = $_POST['lName'];
# Create output string
$str = "\"$fName\" , \"$lName\"\n ";
# Open and write to file
$fh = fopen("postData .txt", "a+");
fwrite($fh, $str);
fclose($fh);
echo "done";
?>
[/code]
Any thoughts?
Thanks.
Comment