URLConnection
中有setDoOutput()
。根据documentation我应该
Set the DoOutput flag to true if you intend to use the URL connection for output, false if not.
现在我面对的是 exactly this problem - 一旦 setDoOutput(true)
被调用,Java 运行时将请求转换为 POST
并且服务器只响应 GET
请求。我想了解如果我从代码中删除该 setDoOutput(true)
会发生什么。
这究竟会影响什么?假设我将它设置为 false
- 我现在可以做什么,现在不能做什么?我可以执行 GET
请求吗?这种方法的上下文中的“输出”是什么?
最佳答案
如果要发送(输出)请求正文,例如使用 POST 或 PUT 请求,则需要将其设置为 true。使用 GET,您通常不会发送正文,因此您不需要它。
发送请求正文本身是通过连接的输出流完成的:
conn.getOutputStream().write(someBytes);
关于java - URLConnection.setDoOutput() 究竟有什么影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8587913/