1

I have researched extensively and cannot find a solution. I have been using the solutions provided to other users and it does not seem to work for me.

My java code:

public class Post {
public static void main(String[] args) { 
    String name = "Bobby";
    String address = "123 Main St., Queens, NY";
    String phone = "4445556666";

    String data = "";
    try { 
        // POST as urlencoded is basically key-value pairs
        // create key=value&key=value.... pairs
        data += "name=" + URLEncoder.encode(name, "UTF-8");
        data += "&address=" + 
            URLEncoder.encode(address, "UTF-8");
        data += "&phone=" + 
            URLEncoder.encode(phone, "UTF-8");

        // convert string to byte array, as it should be sent
        byte[] dataBytes = data.toString().getBytes("UTF-8");

        // open a connection to the site
        URL url = new URL("http://xx.xx.xx.xxx/yyy.php");
        HttpURLConnection conn = 
            (HttpURLConnection) url.openConnection();

        // tell the server this is POST & the format of the data.
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestMethod("POST");
        conn.setFixedLengthStreamingMode(dataBytes.length);
        conn.getOutputStream().write(dataBytes);

        conn.getInputStream();
        // Print out the echo statements from the php script
        BufferedReader in = new BufferedReader(
                new InputStreamReader(url.openStream()));

        String line;
        while((line = in.readLine()) != null) 
            System.out.println(line);

        in.close();
    } catch(Exception e) { 
        e.printStackTrace();
    }
}
}

and the php

<?php
echo $_POST["name"];
?>

The output I receive is an empty line. I tested to see if it was a php/server side issue by making an html form that sends data over to a similar script and prints the data on the screen and that worked. But, for the life of me, I cannot get this to work with a remote client. I am using Ubuntu server and Apache. Thank you in advance.

1

1 Answer 1

2

The problem is actually in what you read as output. You are doing two requests: 1)conn.getInputStream(); - sends POST request with desired body

2)BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); - sends empty GET request (!!)

Change it to:

// ...
conn.getOutputStream().write(dataBytes);

BufferedReader in = new BufferedReader(
         new InputStreamReader(conn.getInputStream()));

and see result.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it! Thank you so much. Looking at the apache logs, you are correct. I was sending an empty GET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.