如何配置 HTTPServer 使用内容长度而不传输编码:分块?

发布于 2024-11-27 08:23:18 字数 376 浏览 2 评论 0原文

我正在使用 java 的 HTTP Server 对象以及由 WebServiceProvider 实现的 Web 服务。 我发现无论客户请求如何,答案都是分块的,我需要它具有内容长度。 所以我假设问题出在服务器而不是网络服务器提供商,对吧? 我如何配置http标头以使用内容长度而不是分块?

    HttpServer m_server = HttpServer.create();
    Endpoint ep= Endpoint.create(new ep());
    HttpContext epContext = m_server.createContext("/DownloadFile");
    ep.publish(downloadFileContext);

I'm using java's HTTP Server object with web service implemeted by WebServiceProvider.
I see that no matter of the client request, the answer is chunked and i need it to be with content length.
so i'm assuming the problem is in the server and not the web server provider, right?
and how can i configure the http header to use content length and not chunked?

    HttpServer m_server = HttpServer.create();
    Endpoint ep= Endpoint.create(new ep());
    HttpContext epContext = m_server.createContext("/DownloadFile");
    ep.publish(downloadFileContext);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-12-04 08:23:18

我假设您正在谈论 com.sun.net.httpserver HTTP 服务器。我进一步假设您通过调用 Endpoint.publish,使用一些支持HTTPServer的服务提供者。

关键在于 HttpExchange.sendResponseHeaders 方法:

如果响应长度参数大于零,则指定要发送的确切字节数,并且应用程序必须发送该确切数量的数据。如果响应长度参数为零,则使用分块传输编码并且可以发送任意量的数据。应用程序通过关闭 OutputStream 来终止响应正文。

因此,只要处理程序为 responseLength 传递正值,就会使用 Content-Length。当然,要做到这一点,它必须提前知道要发送多少数据,但它很可能不会。恐怕它是否完全取决于绑定的实现。我不相信这是标准化的 - 事实上,我不相信 WebServiceProvider/HTTPServer 是标准化的。

但是,即使您的提供商不合作,您也有一个追索权:编写 Filter 添加缓冲,并将其添加到您用于发布服务的 HttpContext 中。我认为要做到这一点,您必须编写 HttpExchange 缓冲写入其中的数据,将其传递到过滤器链以供处理程序写入其响应,然后当它返回时,写入缓冲内容,设置responseLength 当它这样做时。

I assume you're talking about the com.sun.net.httpserver HTTPServer. I further assume that you're connecting the server to the service with a call to Endpoint.publish, using some service provider which supports HTTPServer.

The key is in the HttpExchange.sendResponseHeaders method:

If the response length parameter is greater than zero, this specifies an exact number of bytes to send and the application must send that exact amount of data. If the response length parameter is zero, then chunked transfer encoding is used and an arbitrary amount of data may be sent. The application terminates the response body by closing the OutputStream.

So, as long as the handler is passing a positive value for responseLength, Content-Length is used. Of course, to do that, it will have to know how much data it is going to send ahead of time, which it might well not. Whether it does or not depends entirely on the implementation of the binding, i'm afraid. I don't believe this is standardised - indeed, i don't believe that the WebServiceProvider/HTTPServer is standardised at all.

However, even if your provider is uncooperative, you have a recourse: write a Filter which adds buffering, and add it to the HttpContext which you are using to publish the service. I think that to do this, you would have to write an implementation of HttpExchange which buffers the data written to it, pass that down the filter chain for the handler to write its response to, then when it comes back, write the buffered content, setting the responseLength when it does so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文