3

I have a Delphi 6 application that uses an Indy TIdTCPClient instance to communicate with a web server. The reason I am not using an HTTP client directly is because the the server is an image streaming server that uses the same socket connection for receiving the command to start streaming as it does to start "pushing" images back to you. In other words, after you send it a typical HTTP POST request, it replies with an HTTP response, and immediately after that it starts sending out a stream of JPEG images.

I already know how to craft a proper POST request and send it using the TIdTCPClient WriteBuffer() method and then use the ReadBuffer() method to receive reply data. What I'd like to do instead is to send a POST request and then ask Indy to wait for a typical HTTP response including retrieving all the bytes in the response body if there is a Content-Length header variable. I of course want it to leave the JPEG frames intact that may have piled in after the HTTP response in the receive queue until I start requesting them (that is, I don't want it including any of the JPEG frames in the HTTP response to my streaming request command until I ask for them using a successive read call).

Is there a method that I can call on a TIdTCPClient that will retrieve completely a typical HTTP response with body content, and nothing else? I thought about using SendCmd() and checking the LastCmdResult property (type: TIdRFCReply) for the response, but I can't tell from the Indy documentation if it retrieves the response body content too if there is a Content-Length header variable as part of the response it returns, nor can I tell if it leaves the rest of the receive queue after the response intact.

What is the best way to accomplish this mixed mode interaction with an HTTP web server that pushes out a stream of JPEG frames right after you make the HTTP request to start streaming?

Also, if there is a clever way to have Indy split the frames using the JPEG frame WINBONDBOUDARY delimiting string, rather than accumulating blocks of data and parsing them out myself, please share that technique.

4

1 回答 1

2

读取 HTTP 响应的正确方法是首先逐行读取以 CRLF 分隔的响应标头,直到遇到空行,即 CRLF+CRLF 序列,然后您可以使用这些标头来决定如何读取剩余的响应数据。标头不仅会告诉您正在发送哪种流(通过Content-Type标头),还会告诉您数据是如何被构建的(Content-Length, Transfer-Encoding: chunked,特定于特定的东西Content-Type等)。

要接收标头,您可以使用连接的Capture()方法,将其ADelim参数设置为空字符串。

之后如何读取剩余数据取决于流的实际格式/框架。在不确切知道您正在接收什么样的流的情况下,没有办法建议您如何最好地阅读它,因为 HTTP 服务器使用了几种不同类型的流协议,其中大多数都没有标准化。提供该信息,然后我/我们可以向您展示如何使用 Indy 实现它。

您不能使用SendCmd()HTTP 协议,因为它没有以与该方法兼容的方式格式化其响应。

于 2012-06-05T00:55:11.907 回答