【发布时间】:2011-11-20 20:46:05
【问题描述】:
我想用 Java 发送一个 POST 请求。目前,我是这样做的:
URL url = new URL("myurl");
URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.println("key=" + URLEncoder.encode("value"));
// we have to get the input stream in order to actually send the request
con.getInputStream();
ps.close();
我不明白为什么我必须调用 con.getInputStream() 才能实际发送请求。如果我不调用它,则不会发送请求。
使用 PrintStream 有问题吗?如果我使用 PrintStream、PrintWriter 或其他东西应该没关系,对吧?
【问题讨论】:
-
尝试用
ps.flush()替换那个电话。 -
我已经尝试过了,但没有帮助。 ps.println() 的每次调用都已经调用了 flush()...
-
对不起,现在我找到了问题的答案:stackoverflow.com/questions/4844535/…