Java, HttpURLConnection and setting the content length

一笑奈何 提交于 2019-12-04 00:19:27

You shouldn't set this header yourself. Use setFixedLengthStreamingMode() or setChunkedTransferMode().

Also do not forget to add a setDoOutput to tell your connection you are going to send data.

I get same error with "Content-Length" -

        URL url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Authorization", authKey);
        connection.setRequestProperty("Content-Length", Integer.toString(requestJSON.getBytes().length));

And I finally deduced that it happens because one of the Json object fields contains diacritical characters.

        someCategory = "Ţepuşă";
        try {
        JSONObject postData = new JSONObject();
        postData.put("category", someCategory);
        .....

That's what the error looks like:

08-27 18:57:07.842 24263-24263/ro.nexuserp.documentecontabile E/Eror: content-length promised 491001 bytes, but received 491000
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!