FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Make a post with JAVA client

 


tonk
I am confused I am trying to make a post with a java client but it is not working. Serever responses say OK but it is not ok.
I wa debbunging and I watched out writes but I don't know what is wrong.
Help me please.
:::::::::::::::::::::::::::::::::::
JAVA client
:::::::::::::::::::::::::::::::::::
import java.net.*;
import java.io.*;


public class Post {

public static void main(String[] args) {

//variables y obejtos
String str;
String post;
DataOutputStream out;
URL url=null;
HttpURLConnection conexion=null;

str="http://localhost/applet/post.php";
post="cadena=ya_haz_el_post";


try
{url=new URL(str);}
catch(MalformedURLException exurl)
{System.err.println("ERROR en el URL java dice: \n\t"+exurl);}

try{
conexion=(HttpURLConnection)url.openConnection();
conexion.setRequestMethod("POST");
conexion.setRequestProperty("Content-type", "application/x-form-urlencode");
conexion.setRequestProperty("Content-Length", Integer.toString(post.length() ) );
conexion.setDoOutput(true);
conexion.setDoInput(true);

out=new DataOutputStream(conexion.getOutputStream() );
out.writeUTF(post);
out.flush();

if ( conexion.getResponseCode() != HttpURLConnection.HTTP_OK )
System.out.println(" Ya");
else {
System.err.println(" NO Server Response:"+conexion.getResponseMessage() );
return;
}

conexion.disconnect();

}
catch(IOException exio)
{System.err.println("ERROR java dice: \n\t"+exio);}


}

}
==========================
This makes out:
NO Server Response:Ok
And in $argc is =0 in the following php
=======================
I also tried with a perl script
::::::::::::::::::::::::::::::::::::::::::::::::::::
PHP script
::::::::::::::::::::::::::::::::::::::::::::::::::::

<?php
$argc=count($_POST);


$str=$_POST['cadena'];

$str=utf8_decode($str);

if($argc==0)
$str= "NO ARGC=0" ;

else if( ($argc>0) &&($str) )
$str="YA".$str;


else
$str="UNKNOW" ;

$hFILE=fopen("/tmp/archi.txt","w+");

if( fwrite($hFILE,$str)==FALSE )
die("NO");


?>

::::::::::::::::::::::::::::::::::::::
access.log
::::::::::::::::::::::::::::::::::::::
127.0.0.1 - - [13/Nov/2006:01:25:29 -0600] "POST /applet/post.php HTTP/1.1" 200 - "-" "Java/1.5.0_07"
muggle
Here you are the minimal set of changes needed in order your application to start writing some valuable information in `/tmp/archi.txt`

tonk wrote:
...
//DataOutputStream out;
OutputStreamWriter out;
...
//conexion.setRequestProperty("Content-type", "application/x-form-urlencode");
conexion.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); ...
//out=new DataOutputStream(conexion.getOutputStream() );
//out.writeUTF(post);
out=new OutputStreamWriter(conexion.getOutputStream() );
out.write(post);
...

If you wonder why your Java application displays this confusing error message, which by the way will continue to be displayed, even after applying the changes suggested. That's because the result of the following test is incorrectly interpreted:
tonk wrote:
...
if ( conexion.getResponseCode() != HttpURLConnection.HTTP_OK )
...

If the result of the above comparison is false, as in your case, it means that response code IS equal to HTTP_OK, i.e. Instead of displaying error message and leaving the function prematurely you should display the other message and continue by closing the connection.
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.