1

我正在.NET 中编写一个简单的 HTTP 客户端用于学习目的。我正在使用最终使用Winsock的 .NET Socket类。我不想使用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

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

4

2 回答 2

1

根据RFC 2616 第 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
于 2013-10-06T17:26:26.997 回答
0

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

于 2013-10-05T16:09:31.003 回答