我正在.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
这是一种或多或少的正确方法吗?
答案 0 :(得分:0)
您应该查看http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-24.html#message.body.length(如果这不能回答您的问题,请发送回工作组。)
答案 1 :(得分:0)
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