Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection FileNotFoundException on large request properties

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.

like image 231
Federico Alvarez Avatar asked Nov 21 '25 16:11

Federico Alvarez


1 Answers

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.

like image 75
Lothar Avatar answered Nov 24 '25 07:11

Lothar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!