Posting through HTTP POST in Applet

Discussion in 'Java' started by vidhi, Mar 30, 2006.

  1. vidhi

    vidhi Guest

    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
     
    vidhi, Mar 30, 2006
    #1
    1. Advertising

  2. vidhi

    Ravi Guest

    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 .
     
    Ravi, Mar 30, 2006
    #2
    1. Advertising

  3. vidhi

    vidhi Guest

    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
     
    vidhi, Mar 30, 2006
    #3
  4. vidhi

    Ravi Guest

    what abt the attribute,value concepts ? is it correct in your
    programme?
     
    Ravi, Mar 30, 2006
    #4
  5. vidhi

    vidhi Guest

    pls can u discribe it with some example, so that i can understand eaisly
     
    vidhi, Mar 30, 2006
    #5
  6. vidhi

    Ravi Guest

    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
     
    Ravi, Mar 30, 2006
    #6
  7. vidhi

    Oliver Wong Guest

    "vidhi" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Mar 30, 2006
    #7
  8. vidhi

    Bruce Lee Guest

    "Ravi" <> wrote in message
    news:...
    > 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.
     
    Bruce Lee, Mar 30, 2006
    #8
  9. vidhi

    Timo Stamm Guest

    Bruce Lee schrieb:
    > "Ravi" <> wrote in message
    > news:...
    >> 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.


    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
     
    Timo Stamm, Mar 30, 2006
    #9
  10. vidhi

    Guest

    vidhi wrote:
    > 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.
     
    , Apr 4, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alanb
    Replies:
    2
    Views:
    562
    alanb
    Apr 23, 2004
  2. ppcdev
    Replies:
    0
    Views:
    503
    ppcdev
    Jan 13, 2004
  3. computer_guy
    Replies:
    3
    Views:
    789
    computer_guy
    Jul 20, 2007
  4. John Dow

    How to receive/send XML file through HTTP Post?

    John Dow, Oct 9, 2007, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    746
    JIGNESH
    Oct 9, 2007
  5. Chris Morris
    Replies:
    2
    Views:
    140
    Chris Morris
    Sep 10, 2003
Loading...

Share This Page