如何逐行流式传输GET请求?

时间:2014-10-31 07:04:04

标签: php c++ http stream request

我想向几乎实时'实时流式传输的服务器发送GET请求。使用Chunk Transfer Encoding,我可以逐行修改。    例如:

SendChunks = SomeHTTPLibrary.SendData;
SendChunks(Example.org, "5\r\n")
SendChunks(Example.org, "Hello\r\n")
SendChunks(Example.org, "7\r\n")
SendChunks(Example.org, "Goodbye\r\n")
SendChunks(Example.org, "0\r\n")

我现在在哪里,我甚至不在乎听取回应。它不需要使用C ++,我对Python,Javascript,PHP或类似的东西感到满意。

1 个答案:

答案 0 :(得分:1)

首先,您不应该发送请求正文以及GET请求。我认为你可以,但如果服务器对它做任何事情,那么它就不合规了。请参阅https://stackoverflow.com/a/983458/241294

从你的问题看起来好像你已经知道你需要分块传输编码。下面是一个粗略的例子,说明如何在python中实现这一点,但是使用POST请求而不是GET请求(来自here的代码):

import httplib

conn = httplib.HTTPConnection('Example.org')
conn.connect()
conn.putrequest('POST', '/post')
conn.putheader('Transfer-Encoding', 'chunked')
conn.endheaders()

conn.send("5\r\n")
conn.send("hello\r\n")
conn.send("7\r\n")
conn.send("Goodbye\r\n")
conn.send("0\r\n")

resp = conn.getresponse()
print(resp.status, resp.reason, resp.read())
conn.close()

有关python分块函数的更好示例,请参阅How to force http.client to send chunked-encoding HTTP body in python?

相关问题