简体   繁体   English

如何告诉HTTP服务器不发送分块编码

[英]How to tell the HTTP server to not send chunked encoding

I am currently writing a HTTP client to do a HTTP POST on a URL that returns a HTTP response. 我目前正在编写HTTP客户端,以便在返回HTTP响应的URL上执行HTTP POST。

However, for error messages code 400 and 500, it sends back non chunked HTTP response, and for success messages, 201, it sends a chunked response. 但是,对于错误消息代码400和500,它发送回非分块的HTTP响应,对于成功消息201,它发送分块响应。

In the request, I am setting the content-length, so I am not sure why it is still sending us the chunked transfer encoding. 在请求中,我正在设置内容长度,所以我不确定为什么它仍然向我们发送分块传输编码。 Is there any other header I can set in the request, that will tell the HTTP server not to send chunked encoding? 我可以在请求中设置任何其他标头,这将告诉HTTP服务器不发送分块编码吗?

        headerList.append("POST /v2/charges HTTP/1.1")
        headerList.append("Content-Type: application/json")
        headerList.append("host: xxxxxxxxx")
        headerList.append("request-id: ABCD001123")
        headerList.append("Content-length: %d" %len(Msg))
        hostReqHeader = "\r\n".join(headerList)
        reqData = hostReqHeader + '\r\n\r\n' + qbPosMsg

I am using sockets to send these HTTP messages, and not using httplib or requests library. 我使用套接字发送这些HTTP消息,而不是使用httplib或请求库。

Chunked is a required feature of HTTP/1.1. Chunked是HTTP / 1.1的必需功能。 If you do not require any other 1.1-specific features, specify HTTP/1.0 in your request: 如果您不需要任何其他1.1特定功能,请在您的请求中指定HTTP / 1.0:

    headerList.append("POST /v2/charges HTTP/1.0")

The Content-Length header you are specifying in your request applies to the request, not the server's response. 您在请求中指定的Content-Length标头适用于请求,而不是服务器的响应。

Chunked transfer-encoding is never used for an HTTP request, only for a response, and only when the client specifies HTTP/1.1 as the protocol. Chunked transfer-encoding从不用于HTTP请求,仅用于响应,并且仅在客户端指定HTTP / 1.1作为协议时使用。

If your client cannot support chunked responses, simply specify HTTP/1.0 as the protocol in your request (replace HTTP/1.1 with HTTP/1.0 in your code). 如果您的客户端不支持分块响应,只需在请求中指定HTTP / 1.0作为协议(在代码中用HTTP/1.0替换HTTP/1.1 )。

There is nothing wrong with using HTTP 1.0 still. 使用HTTP 1.0仍然没有错。 HTTP 1.0 remains quite useful for the ability to write simple clients with a few lines of code, that can still query all modern web servers. HTTP 1.0对于使用几行代码编写简单客户端的能力仍然非常有用,它仍然可以查询所有现代Web服务器。 So it's quite appropriate in this situation. 所以在这种情况下这是非常合适的。 I think that is the beauty of HTTP, that the basic protocol is so simple. 我认为这就是HTTP之美,基本协议非常简单。 HTTP 1.1 and HTTP 2.0 add progressively more complexity in terms of being able to write a client that supports them, but all that complexity is optional - HTTP 1.0 can still be used. HTTP 1.1和HTTP 2.0在能够编写支持它们的客户端方面逐渐增加了复杂性,但所有这些复杂性都是可选的 - 仍然可以使用HTTP 1.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM