2013-02-17 70 views
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()的代码并读取所有输入以使其工作。为什么是这样?如果我只想发送请求而不接收请求,我该怎么办?

回答

0

你不这样做,但你必须打电话getInputStream()getResponseCode()。否则,不会发送任何内容,但否则您无法知道该呼叫是否成功。

相关问题