PDA

View Full Version : Trouble Sending using POST over Http Connection


baran_khan
04-30-2008, 04:49 AM
Hi,

I am trying to send an xml String using HTTP Post method. The connection goes well and i receive HTTP_OK as response, but dont know why its not executing the php script where I am sending the data.

Presently I am testing by creating an empty text file on the server side when I hit the page with the POST data.

Here is the code for the two

PHP Code

<?
if($_POST !=NULL){
$file = fopen("text.txt","w+");
}
else{
echo "M here";
}
?>

Here is the code for the connection....




String response;
HttpConnection conn = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
conn = (HttpConnection) Connector
.open("URL",Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
out = conn.openDataOutputStream();
out.write("barankhan".getBytes());
out.flush();
out.close();
Dialog.alert("rest"+conn.getResponseCode()+HttpConnection.HTTP_BAD_REQUEST);

in = conn.openDataInputStream();
byte[] data = new byte[200];
int numBytes = in.read(data);
StringBuffer responseBuffer = new StringBuffer();
in.close();
conn.close();
for (int i = 0; i < numBytes; i++) {
responseBuffer.append((char) data[i]);
}
response = responseBuffer.toString();
Dialog.alert(response);
} catch (IOException e) {
System.out.println("Here is exception" + e.getMessage());
}



Hope to hear something great!!!:smile:

Ivanov
04-30-2008, 05:00 AM
hi.
please use [code] tags - it makes your code much easier to read.

have you tried to make y post using another client? webpage in the desktop browser?

is your php code executed?

baran_khan
04-30-2008, 05:05 AM
Hi,

Thanks for the reply,

Yeah the PHP code is running perfectly on local machine...I have uploaded it to the server so that I can access it through blackberry. The thing is The code executes without any issues and I get HTTP 200 as the response i.e. HTTP_OK. But the file is not created, so I guess I am able to hit the URL but not able to assign the post variable.

I tried inputStream on the page and received the else part of the page executed as response.

Thanks for the reply!!

Ivanov
04-30-2008, 05:21 AM
try adding

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

baran_khan
04-30-2008, 05:24 AM
Nope:-(

I did tried earlier...not working...i tried to send simple string, xml with the mentioned content type..but not working....

Ivanov
04-30-2008, 06:39 AM
you have to send a key-value pair in your post like "name=barankhan"

this works for me:


<?php
$file = fopen("postvars.txt", "w");

foreach($_POST as $key => $value) {
print($key." => ".$value."\n");
fwrite($file, $key." => ".$value."\n");
}

fclose($file);
?>



...
conn = (HttpConnection) Connector.open(url,Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = conn.openDataOutputStream();
out.write("name=me".getBytes());
out.flush();
out.close();

...

baran_khan
04-30-2008, 07:28 AM
just how do u get to a thing I dont even think of......it worked like anything...Dont know how I can pay back....I really appreciate ur efforts..thanks!!!

Ivanov
04-30-2008, 07:40 AM
you are welcome :)

jfisher
04-30-2008, 07:41 AM
----

baran_khan
05-06-2008, 08:12 AM
Okey,

Here is new thing about sending data over HTTP Connection. Now the requirement is to send an Image over the connection. I have managed to send it in chunks using Base64 encoding in the form of string, but when i asseble the same on the other side, it gets corrupted.

Any intelligent guesses about how we can achieve it.....8-)