How to configure HTTPServer to use content length and not transfer encoding: chunked?

一世执手 提交于 2019-12-13 19:16:22

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/6918016/how-to-configure-httpserver-to-use-content-length-and-not-transfer-encoding-chu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!