1
我有一些代码,从Java小程序发送POST请求PHP脚本:为什么我需要getInputStream为HttpUrlConnection发送请求?
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();
但是,这并不在发送请求工作。我必须添加调用getInputStream()的代码并读取所有输入以使其工作。为什么是这样?如果我只想发送请求而不接收请求,我该怎么办?