当一个HTTP响应完了吗? [英] When is an HTTP response finished?
问题描述
我写一个简单的HTTP客户端.NET学习之用。我现在用的是.NET 插座类,最终使用的Winsock 。我不想使用的WebRequest ,<一个HREF =http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx相对=nofollow> HttpWebRequest的或的 HttpClient的类,因为它们使用的 的WinINet,我不想用,因为我这样做对我自己的理解是如何HTTP工作。
I am writing a simple HTTP client in .NET for learning purposes. I am using the .NET Socket class, which ultimately uses Winsock. I do not want to use the WebRequest, HttpWebRequest, or HttpClient classes, as they use WinINet, which I do not want to use as I am doing this for my own understanding of how HTTP works.
我想知道如何确定何时HTTP响应结束。通过读取HTTP / 1.1规范( 2616 ),我想下面的伪code是如何确定当HTTP响应结束。
I am wondering how to determine when an HTTP response is finished. By reading the HTTP/1.1 specification (RFC 2616), I think the following pseudocode is how to determine when an HTTP response is finished.
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
这是一个更多或更少正确的做法?
Is this a more-or-less correct approach?
推荐答案
您的理解是基本正确的,有一些小的修正,每次的 2616第4.4节:
Your understanding is mostly correct, with some minor corrections, per 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
这篇关于当一个HTTP响应完了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!