Posting through HTTP POST in Applet

V

vidhi

hi,
i m making an applet which redirects my page to a php page on which i
wish to send some information by using HTTP POST; but i faces problem
while doing this, the get posted but information dose'nt.

code which i use is

import javax.swing.*;
import java.net.*;
import java.io.*;

public class MyApplet extends JApplet
{
public void init()
{
URL url=null;
try{
url=new URL("http://192.168.5.22/java_scan/Mp3Project/server.php");
}catch(MalformedURLException me){}
try{
HttpURLConnection
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("Name=vidhi\r\n");
out.close();

}catch(IOException ie){}
getAppletContext().showDocument(url);
}
}


And the code for PHP on which i m listning is

<?php
print <<<EOM
<html>
<body>
$_POST[Name]
</body>
</html>
EOM
;
?>


plese help me to rectify the problem
 
R

Ravi

Is is working for GET method?
Seems be that 'Name=vidhi' is passing as a information in body not in
header. But in PHP you are trying to get like a value from attribute
passed in header. Am I correct ?

In case of attribute,values in header , you can append the string with
URL and send .
 
V

vidhi

i already implement it; but problem in that case if the url is too
large htemsome browser do not accept it, so this will be POST method
 
R

Ravi

I want the evidence of similar examples for the following statment :

PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("Name=vidhi\r\n");

Bascially when you submit a form , the attributes and vlaues like
name1=value1& name2=value2 are send in header. Correct?
Remaining information like your HTML content are send in the Body.
Correct If I am wrong at this place.

Here you are sending the Name=vidhi as content in Body. How it is
accessed as attribute,value in the server side.

I feel its better to check any API handling the Header from connection.
So that we can set the header attribute and vlaues.
Are you clear on what I am trying to say
 
O

Oliver Wong

vidhi said:
hi,
i m making an applet which redirects my page to a php page on which i
wish to send some information by using HTTP POST; but i faces problem
while doing this, the get posted but information dose'nt.

code which i use is

import javax.swing.*;
import java.net.*;
import java.io.*;

public class MyApplet extends JApplet
{
public void init()
{
URL url=null;
try{
url=new URL("http://192.168.5.22/java_scan/Mp3Project/server.php");
}catch(MalformedURLException me){}
try{
HttpURLConnection
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("Name=vidhi\r\n");
out.close();

}catch(IOException ie){}
getAppletContext().showDocument(url);
}
}


And the code for PHP on which i m listning is

<?php
print <<<EOM
<html>
<body>
$_POST[Name]
</body>
</html>
EOM
;
?>


plese help me to rectify the problem

It looks like the code in your applet does *NOT* redirect the browser,
but rather the applet is making its own independent connection to the HTTP
server.

- Oliver
 
B

Bruce Lee

Ravi said:
I want the evidence of similar examples for the following statment :

PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("Name=vidhi\r\n");

Bascially when you submit a form , the attributes and vlaues like
name1=value1& name2=value2 are send in header. Correct?



no when you post a form the parameters are in the body.
 
T

Timo Stamm

Bruce said:
no when you post a form the parameters are in the body.

When you "post" a form this is true. The data is sent using the HTTP
POST method and embedded in the body of the request instead of the URL.

You can, however, use the GET method in a form:

<form method="get" ...

The data will be stored in the URL.


But the only way to preserve the encoding of user input is to use the
POST method in combination with "application/x-www-form-urlencoded" as
encoding type. This is default for forms containing a file input. You
can enforce it using the enctype attribute:

<form method="post" enctype="application/x-www-form-urlencoded" ...


You should use a tested 3rd party lib to encode the data properly.


Timo
 
A

andrewthommo

vidhi said:
hi,
i m making an applet

Besides what the other people noted, check this source
and report the stacktraces..
.....
<sscce>
import java.net.*;
import java.io.*;

public class MyApplet
extends java.applet.Applet
{
public void init()
{
URL url=null;
try{
url=new URL("http://192.168.5.22" +
"/java_scan/Mp3Project/server.php");

// there is no point continuing if the URL
// was malformed
HttpURLConnection connection=
(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintWriter out = new
PrintWriter(
connection.getOutputStream() );
out.println("Name=vidhi\r\n");
out.close();

// there is no point attempting to show
// a document if the URL was malformed..
getAppletContext().showDocument(url);
} catch(MalformedURLException me) {
me.printStackTrace();
} catch(IOException ie) {
ie.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
</sscce>

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top