2

I'm trying to implement the following:

  1. Open a POST connection.
  2. Read response code.
  3. Write content.
  4. Read response code.

The second step throws a following exception:

Caused by: java.io.IOException: content-length promised 345286 bytes, but received 0

I understand that 'getResponseCode' will close the writing stream, but i need to find a way of how to read the response code before actually writing anything. I would like to skip the process of writing content to the request body (100 MB) due to the (for an example) 401 code.

4

1 回答 1

1

HTTP 不能那样工作。您需要发送完整的请求,然后才能读取响应代码。在您的情况下,您的响应已承诺 345286 字节的请求正文,但您没有发送任何内容。

正确的方法是执行以下操作:

  1. 打开 POST 连接。
  2. 写内容。
  3. 读取响应代码。

如果您想验证服务器是否允许这样做(以避免 401),您可以先添加一个小的 GET 请求来验证身份验证。

于 2013-10-27T11:58:53.190 回答