开发者

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

开发者 https://www.devze.com 2023-03-24 23:49 出处:网络
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.

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 ser开发者_如何学运维ver 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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消