7

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.

Community
  • 1
  • 1
HGPB
  • 4,185
  • 7
  • 47
  • 83
  • Define "doesn't seem to make it". – user207421 Apr 09 '13 at 23:05
  • @EJP You're quite right that really is a poor turn of phrase. In fact it does make it to the server however, there is no data whatsoever. In comparison to having `setChunkedStreamingMode` removed and my data comes through. There are no errors either, as far as I can see. – HGPB Apr 09 '13 at 23:55
  • Thank you, now define "the data stream is empty". As measured how? – user207421 Apr 11 '13 at 01:07
  • @EJP I read the input using php: `file_get_contents("php://input")` There is no input as far as I can measure. – HGPB Apr 11 '13 at 01:31
  • 2
    What version of PHP? I found this: "PHP prior to 5.3.0 does not implement chunked transfer decoding". Also you need to have `protocol_version=1.1` in php.ini, the default is HTTP 1.0. Source [here](http://php.net/manual/en/context.http.php) – user207421 Apr 11 '13 at 01:49
  • @EJP Nice find. I am however using version 5.3.8 and my server protocol is already using HTTP/1.1. Both I can see in my php setup: phpinfo(). Thanks for the help all the same. – HGPB Apr 11 '13 at 04:38
  • Very similar question: http://stackoverflow.com/q/12071266/2482894 – leo9r Sep 02 '13 at 03:12
  • @ Haraldo : Have you fixed this..? – Rethinavel Sep 19 '13 at 09:39
  • @RethinavelVelu I didn't fix it. I just ended up not using my local machine and switched to a demo server which had no problems. The issue lies somewhere in the server setup I believe. – HGPB Sep 19 '13 at 17:12

3 Answers3

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.

Santiago
  • 91
  • 6
  • 7
    Thanks for the reply, this has the same effect as setting `setChunkedStreamingMode`. No data is received. Really very odd indeed. I had tried this already also. Also I think your answer should read: `urlConnection.setRequestProperty("Transfer-Encoding", "chunked")`. – HGPB Apr 10 '13 at 22:18
  • 1
    It's hard to believe that what you described really solved your problem. `HttpURLConnection` sets that header itself when necessary. – user207421 Apr 11 '13 at 01:44
0

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.

user3666197
  • 1
  • 6
  • 43
  • 77
Daniel
  • 1,957
  • 2
  • 20
  • 35
0

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.

MiguelHincapieC
  • 5,080
  • 5
  • 38
  • 65