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()的代碼並讀取所有輸入以使其工作。爲什麼是這樣?如果我只想發送請求而不接收請求,我該怎麼辦?