我正在 .NET 中编写一个简单的 HTTP 客户端用于学习目的。我正在使用.NETSocket http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx类,最终使用Winsock http://en.wikipedia.org/wiki/Winsock。我不想使用网络请求 http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx, HttpWeb请求 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx, or Http客户端 http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx类,因为他们使用WinINet http://msdn.microsoft.com/en-us/library/windows/desktop/aa383630.aspx,我不想使用它,因为我这样做是为了我自己对 HTTP 工作原理的理解。
我想知道如何确定 HTTP 响应何时完成。通过阅读 HTTP/1.1 规范(RFC 2616 http://www.ietf.org/rfc/rfc2616.txt),我认为下面的伪代码是如何确定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 节 https://www.rfc-editor.org/rfc/rfc2616#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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)