【发布时间】:2013-10-12 12:07:24
【问题描述】:
出于学习目的,我正在.NET 中编写一个简单的 HTTP 客户端。我正在使用.NET Socket 类,它最终使用Winsock。我不想使用WebRequest、HttpWebRequest 或HttpClient 类,因为它们使用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
这是一种或多或少正确的方法吗?
【问题讨论】:
-
请参阅 RFC 2616 §4.4 (tools.ietf.org/html/rfc2616#section-4.4) 了解更多需要考虑的情况。
标签: .net sockets http http-headers