【问题标题】:When is an HTTP response finished?HTTP 响应何时完成?
【发布时间】:2013-10-12 12:07:24
【问题描述】:

出于学习目的,我正在.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

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

【问题讨论】:

标签: .net sockets http http-headers


【解决方案1】:

根据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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2023-03-23
      • 2013-12-10
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多