Delphi - 使用 Indy HTTP 客户端将响应发送回服务器
我正在使用 Indy idHTTP 对象从服务器请求 XML 数据。发出请求后,服务器会发送回复,我可以读取回复标头 OK: 结果:= IdHTTP1.Response.ResponseText;
然后,他们的系统继续定期发送未经请求的 XML 数据,直到预定的超时期限到期。
但是,客户现在希望我的客户端应用程序在每次收到数据时向他们发送 HTTP 响应(基于成功解析 XML 的响应)。
如果我是服务器,我可以弄清楚如何发送响应,但是如何生成响应标头并使用我的 idHTTP1 客户端仅发布(或放置、发送等)标头。
我认为这在 RFC 2616 中没有完全定义,因为服务器通常发送 HTTP 响应,而不是客户端,即:
6 响应
“服务器接收并解释请求消息后做出响应 带有 HTTP 响应消息。”
我可以使用 TIdHTTPResponseInfo 并使用 WriteHeader 方法,但无法弄清楚如何设置与我需要回复的 URL 的链接(如果我是客户端)。
我正在使用 Delphi XE 和Indy 10.5.7
我尝试了多种方法,但没有一个成功。
非常感谢任何帮助或可能的代码示例!
I am using Indy idHTTP object to request XML data from a server. Once the request is made, the server sends a reply, and I can read the reply header OK:
result := IdHTTP1.Response.ResponseText;
Their system then continues to send unsolicited XML data at regular intervals until a predetermined timeout period expires.
However, the customer now wants my client application to send them a HTTP response back every time I receive data (the response based on successful parsing of the XML).
I can work out how to send a response if I was the server, but how can I generate a response header and use my idHTTP1 client to post (or put, send etc) just the header.
This is I think, not exactly defined in RFC 2616, as the server normally sends the HTTP response, not the client, viz:
6 Response
"After receiving and interpreting a request message, a server responds
with an HTTP response message."
I thought I could use TIdHTTPResponseInfo and use the WriteHeader method, but could not work out how to set up the linkage to the URL I need to reply to if I am the client.
I am using Delphi XE and Indy 10.5.7
I've tried a number of ways, but none of them successfully.
Any help or possible code examples are really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题可以分为两部分:(a) 我必须向服务器发送哪些数据来通知服务器,以及 (b) 如何使用 IdHTTP 发送这些数据。
发送数据流直到连接关闭对应于HTTP标准,HTTP是一种请求-响应协议。如果客户端收到数据作为响应,则它无法回复自己的响应 - 客户端只能发送另一个请求。因此,您的任务是弄清楚服务器会接受什么样的请求作为数据接收的确认。一旦你明白了这一点,你将能够发现如何(如果)使用 IdHTTP 实现这一点。
更新:如果您应该向服务器发送 200 OK,那么,这不符合 HTTP 协议规范。您可以尝试访问 HTTP 客户端后面的套接字并仅发送 7 个字节(200 OK + CRLF)。
Your question can be split into two parts: (a) what data do I have to send to the server to notify the server, and (b) how do I send this data using IdHTTP.
While sending the data stream until connection is closed corresponds to HTTP standard, HTTP is a request-response protocol. If the client receives data as a response, then it can't reply with it's own response - the client can only send another request. So your task is to figure out, what kind of request the server would accept as a confirmation of data receipt. Once you figure this, you will be able to find how (if) it is possible with IdHTTP.
Update: If you should send 200 OK to the server, then, this is not conformant to HTTP protocol specification. You can try getting to the socket behind the HTTP client and just send 7 bytes (200 OK + CRLF).
如果服务器期望客户端发送每个 XML 回复的确认,那么服务器就没有实现实际的 HTTP 协议,因为这是不允许的行为。更有可能的是,他们正在实现自己的自定义协议,该协议模仿但不是 HTTP。例如,SIP 具有许多与 HTTP 类似的特征,但也有许多不同的特征(例如对单个请求的多个回复)。
您需要找出服务器真正实现的协议类型。您将无法使用
TIdHTTP
来实现它。直接使用TIdTCPClient
即可。然后您可以根据需要发送和接收数据。If the server expects the client to send an acknowledgement of each XML reply, then the server IS NOT implementing the actual HTTP protocol, as this is not allowed behavior. More likely, they are implementing their own custom protocol that mimics, but is not, HTTP. SIP, for example, has many characterics that are similar to HTTP, and many that are not (multiple replies to single requests, for instance).
You need to find out what kind of the protocol the server is really implementing. You will not be able to use
TIdHTTP
for it. UseTIdTCPClient
directly instead. Then you can send and receive data however you need.