I think your problem is this:
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
I do not see where postDataBytes declared, but since you are processing the parameters in paramsToString , I assume that they are not relevant.
Now I am not an expert on RFC 2616 (HTTP), but I think that the length of postDataBytes greater than the size of your request, so the server does not disconnect the socket at its end, those URLConnection objects are combined, so when you go to get the connection object, its values have been cleaned for reuse, but the actual connection is still open.
Here is the code I think you should try. If this does not fix your problem, I do not receive a reward for the reputation, but it is still definitely more correct than yours:
private static final String CHARSET = "ISO-8859-1"; // or try "UTF-8" public SimpleResponse callPost(String urlTo, Map<String, String> params) { // get rid of this... // System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = null; SimpleResponse response = new SimpleResponse(0, null); try { URL url = new URL(urlTo); conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setConnectTimeout(CONNECTION_TIME_OUT); conn.setReadTimeout(CONNECTION_TIME_OUT); conn.setInstanceFollowRedirects(false); conn.setRequestMethod("POST"); // ... and get rid of this // conn.setRequestProperty("Connection", "close"); conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=" + CHARSET); String content = paramsToString(params); int length = content.getBytes(Charset.forName(CHARSET)).length; conn.setRequestProperty("Content-Length", Integer.toString(length)); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, CHARSET)); writer.write(content); writer.flush(); writer.close(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = conn.getInputStream(); String result = StringUtils.fromInputStream(in); response = new SimpleResponse(responseCode, result); in.close(); } else { response = new SimpleResponse(responseCode, null); } } catch (Exception e) { e.printStackTrace(); } if (conn != null) { conn.disconnect(); } return response; }
I apologize for any compilation errors. I am useless without an IDE. I proved it as best as possible.
I used Latin-1 encoding. If this is not for you, you can try UTF-8.
Another thing you can try is to discard the length of the content and call
conn.setChunkedStreamingMode(0);
And yes, I understand that calling getBytes() and OutputStreamWriter duplicate the same process. You can work on this as soon as you fix this problem.