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

前端 未结 3 403
旧时难觅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:43

    Edit It looks to me like in the 404 situation, you're sending something like this:

    HTTP/1.1 404 Not Found
    Content-Type: text/html
    HTTP/1.1 200 OK
    Content-Length: 1234
    Content-Type: text/html
    

    ...followed by the 404 page. Note the 200 line following the 404. This is because your 404 handling is calling sendFile, which is outputting the 200 response status code. This is probably confusing the receiver.

    Old answer that missed that:

    An HTTP response starts with a status line followed (optionally) by a series of headers, and then (optionally) includes a response body. The status line and headers are just lines in a defined format, like (to pick a random example):

    HTTP/1.0 404 Not Found
    

    To implement your small HTTP server, I'd recommend having a read through the spec and seeing what the responses should look like. It's a bit of a conceptual leap, but they really are just lines of text returned according to an agreed format. (Well, it was a conceptual leap for me some years back, anyway. I was used to environments that over-complicated things.)

    It can also be helpful to do things like this from your favorite command line:

    telnet www.google.com 80
    GET /thispagewontbefound
    

    ...and press Enter. You'll get something like this:

    HTTP/1.0 404 Not Found
    Content-Type: text/html; charset=UTF-8
    X-Content-Type-Options: nosniff
    Date: Sun, 12 Sep 2010 23:01:14 GMT
    Server: sffe
    Content-Length: 1361
    X-XSS-Protection: 1; mode=block
    

    ...followed by some HTML to provide a friendly 404 page. The first line above is the status line, the rest are headers. There's a blank line between the status line/headers and the first line of content (e.g., the page).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-26 15:52

    Based on your reported symptoms, I think the real problem is that you are not actually talking to your server at all! The evidence is that 1) you cannot get a 404 response, and 2) a 200 response does not have the content length and type. Neither of these should be possible ... if you are really talking to the code listed above.

    Maybe:

    • you are talking to an older version of your code; i.e. something is going wrong in your build / deploy cycle,
    • you are (mistakenly) trying to deploy / run your code in a web container (Jetty, Tomcat, etc), or
    • your client code / browser is actually talking to a different server due to proxying, an incorrect URL, or something like that.

    I suggest that you add some trace printing / logging at appropriate points of your code to confirm that it is actually being invoked.

    0 讨论(0)
提交回复
热议问题