什么时候HTTP响应完成?

时间:2013-10-05 14:53:16

标签: .net sockets http http-headers

我正在.NET中编写一个简单的HTTP客户端用于学习目的。我正在使用.NET Socket类,它最终使用Winsock。我不想使用WebRequestHttpWebRequestHttpClient类,因为他们使用WinINet,我不想使用它,因为我这样做是为了我的自己了解HTTP的工作原理。

我想知道如何确定何时完成HTTP响应。通过阅读HTTP / 1.1规范(RFC 2616),我认为以下伪代码是如何确定何时完成HTTP响应。

parse HTTP headers
if parse not successful:
    throw error
if HTTP version is 1.1 and Transfer-encoding is chunked:
    parse first line of each chunk as an ASCII hexadecimal, the chunk size
    if parse not successful:
        throw error
    read each chunk until chunk size 0
else if Content-Length is specified:
    read Content-Length number of bytes
else:
    throw error

这是一种或多或少的正确方法吗?

2 个答案:

答案 0 :(得分:0)

您应该查看http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-24.html#message.body.length(如果这不能回答您的问题,请发送回工作组。)

答案 1 :(得分:0)

根据RFC 2616 Section 4.4

,您的理解大多是正确的,并进行了一些小的更正
Read and parse HTTP headers
if not successful:
    throw error
if response can contain message body:
    if HTTP version is 1.1+ and Transfer-encoding is not identity:
        while true:
            read line, extract delimited ASCII hexadecimal, the chunk size
            if not successful:
                throw error
             if chunk size is 0:
                break while loop
             read chunk size number of bytes
        read and parse trailing HTTP headers
    else if Content-Length is specified:
        read Content-Length number of bytes
    else if Content-Type is "multipart/byteranges":
        read and parse MIME-encoded chunks until terminating MIME boundary is reached
    else:
        read until connection is closed
相关问题