我写为HTTP / HTTPS请求原始TCP客户端,但是我在使用分块编码的响应的问题。 HTTP / 1.1是要求因此,我应该支持。

原始TCP是一个业务需求,我需要继续,所以我不能切换到.NET的HttpWebRequest / HTTPWebResponse 的但是如果有这样一个原始的HTTP请求/响应转换成的HttpWebRequest / HTTPWebResponse那会工作。

有帮助吗?

解决方案

开始的最佳位置是在 HTTP 1.1规范,该规定如何分块工程。具体地说部分3.6.1。

  

3.6.1分块传输编码

     

在分块编码修改   的消息,以搜索体   转让其作为一系列块,   每个都有自己的规模指标,结果   跟着一个可选的拖车   含实体头字段。这个   允许动态生成的内容,以   与结果一起被转移   必要的信息   收件人验证它已点击   接收到完整的消息。

   Chunked-Body   = *chunk
                    last-chunk
                    trailer
                    CRLF

   chunk          = chunk-size [ chunk-extension ] CRLF
                    chunk-data CRLF
   chunk-size     = 1*HEX
   last-chunk     = 1*("0") [ chunk-extension ] CRLF

   chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
   chunk-ext-name = token
   chunk-ext-val  = token | quoted-string
   chunk-data     = chunk-size(OCTET)
   trailer        = *(entity-header CRLF)
     

在块大小字段是一串   十六进制数字显示结果的大小   块。在分块编码是   任何块大小为结果结束   零,其次是拖车,这   通过一个空行终止。

     

在拖车允许发送者   包括附加的HTTP头,点击   在消息的结束字段。该   拖车头字段可以被用来   指示哪些报头字段是   包括在拖车(见   14.40)。

假设您已经阅读从响应头,并指向下一个字节流中的伪代码看起来是这样的:

done = false;
uint8 bytes[];
while (!done)
{
  chunksizeString = readuntilCRLF(); // read in the chunksize as a string
  chunksizeString.strip(); // strip off the CRLF
  chunksize = chunksizeString.convertHexString2Int(); // convert the hex string to an integer.
  bytes.append(readXBytes(chunksize)); // read in the x bytes and append them to your buffer.
  readCRLF(); // read the trailing CRLF and throw it away.
  if (chunksize == 0)
     done = true; //

}
// now read the trailer if any
// trailer is optional, so it may be just the empty string
trailer = readuntilCRLF()
trailer = trailer.strip()
if (trailer != "")
   readCRLF(); // read out the last CRLF and we are done.

这是忽略了块扩展部分,但由于它被分隔用“;”它应该很容易把它分离出来。这应该足以让你开始。请记住,在CHUNKSIZE字符串的有一个前导 “0x”。

其他提示

有关将来的参考也我发现这一点:

 length := 0
   read chunk-size, chunk-extension (if any) and CRLF
   while (chunk-size > 0) {
      read chunk-data and CRLF
      append chunk-data to entity-body
      length := length + chunk-size
      read chunk-size and CRLF
   }
   read entity-header
   while (entity-header not empty) {
      append entity-header to existing header fields
      read entity-header
   }
   Content-Length := length
   Remove "chunked" from Transfer-Encoding
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top