Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Java POST Data

  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java POST Data

    Hi.

    I am new to java and am having trouble with using java and POST data.

    I am trying to create a simple script that will send a POST parameter to a php file and then display the output.

    My Java script is:

    Code:
    import javax.swing.*;
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    public class PostDataTest extends JApplet {
    	URL url;
    	URLConnection urlConn;
    	DataOutputStream output;
    	BufferedReader input;
    	String error = "";
    	String result = "";
    	
    	public void init() {
    		try {
    			url = new URL(getCodeBase().toString() + "engine.php");
    			
    			urlConn = url.openConnection();
    			urlConn.setDoInput(true);
    			urlConn.setDoInput(true);
    			urlConn.setUseCaches(false);
    			urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    			
    			output = new DataOutputStream(urlConn.getOutputStream());
    			String content = "action=register";
    			output.writeBytes(content);
    			output.flush();
    			output.close();
    			
    			DataInputStream in = new DataInputStream(urlConn.getInputStream());
    			input = new BufferedReader(new InputStreamReader(in));
    			String str;
    			while ((str = input.readLine()) != null) {
    				result = result + str + "\n";
    			}
    			input.close();
    		}
    		catch (MalformedURLException e) {
    			error += e + "\n";
    		}
    		catch (IOException e) {
    			error += e + "\n";
    		}
    	}
    	
    	public void paint(Graphics screen) {
    		Graphics2D screen2D = (Graphics2D)screen;
    		screen2D.drawString("Result: " + result, 10, 10);
    	}
    }
    And my php script is:

    Code:
    <?php
    echo "Action requested: " . $_POST["action"];
    ?>

    After running the script I get the output:

    Result:

    And the contents from the php file are not displayed.

    Anyone have any clue why this isnt working?

    Thanks.

  • #2
    New Coder
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually nevermind I forgot to actually display any errors that come up -_-

    Once I made it display errors I found out that the problem was that I put
    urlConn.setDoInput(true);
    urlConn.setDoInput(true);

    instead of

    urlConn.setDoOutput(true);
    urlConn.setDoInput(true);

    Which makes it work

  • #3
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    i am new to applet programming and i have a question about this script can anyone answer me this.

    if i wanted to add more paired values (POST DATA) do i just add them to

    Code:
    String content = "action=register";
    
    //like
    
    String content = "action=register&name=somename&email=someone@somedomain.com&password=password"
    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •