3

I'm using HttpURLConnection to send JSON data from an Android Application to my Tomcat Server.

The POST works fine with small sized JSONs. On bigger data sets it fails with a FileNotFoundException.

What can it be?

Here's the code:

try {
        URL url = new URL(urlIn);
        strOut = "";
        huc = (HttpURLConnection) url.openConnection();
        huc.setRequestProperty("Connection", "Close");
        huc.setRequestMethod("POST");
        huc.setRequestProperty("User", userId);
        huc.setRequestProperty("Action", action);
        huc.setRequestProperty("JSON", jsonData);
        huc.setConnectTimeout(10000);

        in = new BufferedReader(new InputStreamReader(huc.getInputStream()));
        while ((inputLine = in.readLine()) != null){
            if (strOut.equalsIgnoreCase("")){
                strOut = inputLine;
            } else {
                strOut = strOut + inputLine;
            }
        }
    } catch (Exception e) {
        strOut = "";
        e.printStackTrace();
    }

When jsonData get to a certain size (arround 10000 chars), the POST fails with the error mentioned. The content of the JSON does not have any special character.

Thanks in advance.

Best regards, Federico.

Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30
  • 1
    https://stackoverflow.com/a/2799958/3166697 – Dima Kozhevin Oct 10 '17 at 19:51
  • Thanks Dima, I tried that and other properties of HttpUrlConnection with no luck (setConnectionTimeout(10000), setReadTimeout(10000), setDoOutput(false), setInstanceFollowRedirects(false)). All those ideas where taken from other SO posts. Thanks again. – Federico Alvarez Oct 10 '17 at 19:56
  • 1
    Why do you send the data as part of the HTTP Request Header instead of the request body? That's what's supposed to be done, when doing HTTP-requests? – Lothar Oct 10 '17 at 20:08
  • I agree with you Lothar, I dont know why it is implemented this way, but the server expects it in the header. There is another system that consumes the same POST with large JSON data and works fine. So before suggesting a change in the server I must make sure that in Java it is not possible to achieve. – Federico Alvarez Oct 10 '17 at 20:14
  • 1
    Request property = HTTP header. If you're going to use custom headers, prefix the name with `X-` e.g. `X-Action`. Actual content should be sent in HTTP body as @Lothar suggests in his answer. List of standard HTTP headers is here: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields – Eugen Pechanec Oct 11 '17 at 00:40

2 Answers2

1

HTTPUrlConnection throws a FileNotFoundException if the server responds with a 404 response code, so the reason why this happens seems to be located on the server side rather than the client side. Most likely the server is configured to accept request headers up to a particular length and will return an error if that size is exceeded. A short Google-search brought up a couple of results, sizes of 16 KB are mentioned but shorter values are also reasonable.

As I mentioned in my comment to your question, you should change your process to receive the JSON-data (and the other values for User and Action as well BTW) as part of the request body, e.g. as url-encoded query string or as multipart formdata. Both ways are supported by HTTP client libraries you can use or are easily built manually.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • Thanks again Lothar. I tried to increase that limit in Tomcat but no luck. Added maxHttpHeaderSize="2097152" maxPostSize="2097152" to the connector, but same outcome. – Federico Alvarez Oct 10 '17 at 20:20
  • 1
    both settings have no impact on the maximum size of a single HTTP request header. I don't know Tomcat that much to give you the correct parameter or to be able to say if it's even possible – Lothar Oct 10 '17 at 20:22
  • 1
    You might enable debugging on Tomcat to see if any informations comes up why the response is the way it is. From there you can try to find out a configuration settings that prevents it from happening. – Lothar Oct 10 '17 at 20:27
  • Thanks Lothar. It seems that Tomcat is denying POST as you say. Will keep looking for more parameters to change. – Federico Alvarez Oct 10 '17 at 20:43
0

After lots of reading and trying I gave up with configuring Tomcat to accept larger headers.

So I convinced the team in charge of the Tomcat app to make a servlet that is able to receive this data in the body, just as Lothar suggested.

Thanks!

Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30