HTTP 响应何时完成? [英] When is an HTTP response finished?
问题描述
我正在 .NET 中编写一个简单的 HTTP 客户端以用于学习目的.我正在使用 .NET Socket 类,最终使用 Winsock.我不想使用 WebRequest,
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 规范(RFC 2616),我认为下面的伪代码是如何以确定 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?
推荐答案
根据 RFC 2616 第 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屋!