gpt4 book ai didi

.net - HTTP响应何时结束?

转载 作者:行者123 更新时间:2023-12-03 12:05:29 25 4
gpt4 key购买 nike

我出于学习目的在.NET中编写了一个简单的HTTP客户端。我正在使用.NET Socket类,最终使用了Winsock。我不想使用WebRequestHttpWebRequestHttpClient类,因为它们使用了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 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

关于.net - HTTP响应何时结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19199066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com