How can I send an HTTP Response using only standard network libraries?

前端 未结 3 404
旧时难觅i
旧时难觅i 2021-01-26 15:17

I\'m working on my first homework project in a web programming class, which is to write a simple web server in Java. I\'m at the point where I have data being transmitted back a

3条回答
  •  被撕碎了的回忆
    2021-01-26 15:44

    The problem you are seeing is most likely related to a missing flush() on your writer. Depending on which type of Writer you use the bytes are first written to a buffer that needs to be flushed to the stream. This would explain why Content-Length and Content-Type are missing in the output. Just flush it before you write additional data to the stream.

    Further you call sendFile("/404.html", sock);. You did not post the full method here - but I suppose that you call it recursively inside sendFile and thus send the 200 OK status for your file /404.html.

提交回复
热议问题