Setting setChunkedStreamingMode in HttpURLConnection fails to deliver data to server

巧了我就是萌 提交于 2019-12-19 02:21:59

问题


My server version is as follows on my dev machine:

Apache/2.2.21 (Win32) mod_fcgid/2.3.6

I have been testing HttpURLConnection as my project requires easy streaming capabilties. I have read a great synopsis from @BalusC on how to use the class.

Using java.net.URLConnection to fire and handle HTTP requests

The trouble I am currently having is when setting setChunkedStreamingMode. Regardless of what I set it to my stream doesn't seem to make it to the server the data stream is empty when my server api method/connection is called/made. However, if I remove it, it works fine.

I have seen another person with a similar issue:

Java/Android HttpURLConnection setChunkedStreamingMode not working with all PHP servers

But with no real resolution. I am unable to set it to setFixedLengthStreamingMode simply because the content (json) is variable in length.

This is NOT OK. I potentially will be transfering very large quantities of data and hence cannot have the data stored in memory.

My question is, how can I get setChunkedStreamingMode to play nice? Is it a server setup issue or can it be fixed in code?

EDIT I have now tested my code on my production server and it works no problem. I would however still like to know why my Apache server on my local machine fails. Any help is still much appreciated.


回答1:


Try adding this HTTP header:

urlConnection.setRequestHeader("Transfer-Encoding","chunked");

I haved a problem like this: although I haved set the chunked HTTP streaming mode (urlConnection.setChunkedStreamingMode(0) ), it not worked, but putting the HTTP header above it works fine.




回答2:


I had a similar issue. In my case it was the client system that had a virus scanner installed. Those scanners sometimes have identity theft modules that intercept POSTs, scan the body and then pass it on.

In my case BitDefender cached about 5MB before passing it on.

If the whole payload was less then the POST was delivered as non chunked fixed length request.




回答3:


I had a similar problem using HttpURLConnection. Just add:

conn.setRequestProperty("connection", "close"); // disables Keep Alive

to your connection or disable it for all connections:

System.setProperty("http.keepAlive", "false");

From the API about disconnect():

Releases this connection so that its resources may be either reused or closed. Unlike other Java implementations, this will not necessarily close socket connections that can be reused. You can disable all connection reuse by setting the http.keepAlive system property to false before issuing any HTTP requests.



来源:https://stackoverflow.com/questions/15913844/setting-setchunkedstreamingmode-in-httpurlconnection-fails-to-deliver-data-to-se

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