我有一个简单的POST请求:
val url = URL(SEND_URL)
val connection = url.openConnection() as HttpURLConnection
connection.doOutput = true
connection.addRequestProperty("Authorization", "...")
connection.addRequestProperty("Content-Type", "application/json")
val messageJson = Gson().toJson(message)
val outputStream = OutputStreamWriter(connection.outputStream, "UTF-8")
outputStream.write(messageJson)
outputStream.flush()
outputStream.close()
connection.connect()
它不发送任何东西。
当我添加时(而不是connection.connect())
connection.responseCode
connection.disconnect()
有效。
有人可以向我解释为什么吗?我不需要读取response或responseCode,但是没有它,就不会发送我的请求。
请参阅此问题和答案:
Why does HttpURLConnection not send the HTTP request