Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?

后端 未结 6 1712
情书的邮戳
情书的邮戳 2020-11-27 05:28

I\'m trying to write out to URLConnection#getOutputStream, however, no data is actually sent until I call URLConnection#getInputStream. Even if I set URLConnnection#doInput

6条回答
  •  北海茫月
    2020-11-27 06:05

    The fundamental reason is that it has to compute a Content-length header automatically (unless you are using chunked or streaming mode). It can't do that until it has seen all the output, and it has to send it before the output, so it has to buffer the output. And it needs a decisive event to know when the last output has actually been written. So it uses getInputStream() for that. At that time it writes the headers including the content-length, then the output, then it starts reading the input.

提交回复
热议问题