BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 04-30-2008, 04:49 AM   #1 (permalink)
Thumbs Must Hurt
 
baran_khan's Avatar
 
Join Date: Apr 2008
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 110
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble Sending using POST over Http Connection

Please Login to Remove!

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....


PHP Code:

        String response
;
        
HttpConnection conn null;
        
DataInputStream in null;
        
DataOutputStream out null;
        try {
            
conn = (HttpConnectionConnector
                    
.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();
            
bytexxx91;xxx93; data = new bytexxx91;200xxx93;;
            
int numBytes in.read(data);
            
StringBuffer responseBuffer = new StringBuffer();
            
in.close();
            
conn.close();
            for (
int i 0numBytesi++) {
                
responseBuffer.append((chardataxxx91;ixxx93;);
            }
            
response responseBuffer.toString();
            
Dialog.alert(response);
        } catch (
IOException e) {
            
System.out.println("Here is exception" e.getMessage());
        } 
Hope to hear something great!!!

Last edited by baran_khan : 04-30-2008 at 05:02 AM.
Offline  
Old 04-30-2008, 05:00 AM   #2 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Offline  
Old 04-30-2008, 05:05 AM   #3 (permalink)
Thumbs Must Hurt
 
baran_khan's Avatar
 
Join Date: Apr 2008
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 110
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!!
Offline  
Old 04-30-2008, 05:21 AM   #4 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

try adding
Code:
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Offline  
Old 04-30-2008, 05:24 AM   #5 (permalink)
Thumbs Must Hurt
 
baran_khan's Avatar
 
Join Date: Apr 2008
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 110
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nope

I did tried earlier...not working...i tried to send simple string, xml with the mentioned content type..but not working....
Offline  
Old 04-30-2008, 06:39 AM   #6 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

you have to send a key-value pair in your post like "name=barankhan"

this works for me:

PHP Code:
<?php
$file 
fopen("postvars.txt""w");

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

fclose($file);
?>
PHP Code:
...
conn = (HttpConnectionConnector.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();

... 
Offline  
Old 04-30-2008, 07:28 AM   #7 (permalink)
Thumbs Must Hurt
 
baran_khan's Avatar
 
Join Date: Apr 2008
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 110
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!!!

Last edited by baran_khan : 05-01-2008 at 04:34 AM.
Offline  
Old 04-30-2008, 07:40 AM   #8 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

you are welcome
Offline  
Old 04-30-2008, 07:41 AM   #9 (permalink)
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

----
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
Offline  
Old 05-06-2008, 08:12 AM   #10 (permalink)
Thumbs Must Hurt
 
baran_khan's Avatar
 
Join Date: Apr 2008
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 110
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Send Image over HTTP Connection

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.....
__________________
Smart People ask for Help!!!
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.