1

I have some code that sends a POST request to a PHP script from a Java applet:

String message = URLEncoder.encode(s, "UTF-8");
URL url = new URL(getCodeBase(), "script.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("message=" + message);
out.close();

But this doesn't work in sending the request. I have to add code that calls getInputStream() and reads all of the input for this to work. Why is this? What do I do if I only want to only send a request and not receive one?

4

1 回答 1

0

您不需要,但您必须调用getInputStream()getResponseCode()。否则不会发送任何内容,否则您将无法知道调用是否成功。

于 2013-02-17T09:02:57.823 回答