I am trying to connect to the server using HttpURLConnection , but I have a problem with the PUT method.
I need to send a string with 1500 characters (or more), but in this case the server issues a timeout and returns 500 - an internal server error .
If I send a string less than 1400 characters, I have no problem and the server returns OK .
My code is as follows:
public String connectToServer(String prototype) { String responseString = ""; try { BufferedReader in = new BufferedReader(new InputStreamReader(openURLForInput(new URL(URL), USERNAME, PASSWORD, prototype))); String line; while ((line = in.readLine()) != null) { System.out.println(line); responseString += line; } } catch (IOException e) { e.printStackTrace(); responseString = e.toString(); } return responseString; }
// -----------------------
public InputStream openURLForInput(URL url, String uname, String pword, String content) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Authorization", userNamePasswordBase64(uname, pword)); // I know this is OK conn.addRequestProperty("Content-type", "application/xml; charset=utf-8"); //conn.setChunkedStreamingMode(8 * 1024); conn.setRequestMethod("PUT"); conn.connect(); OutputStream output = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(output, "UTF-8"); BufferedWriter writer = new BufferedWriter(osw); writer.write(content); // content length > 1400 characters writer.close(); output.close(); int status = conn.getResponseCode(); Log.i("STATUS", status + ""); Log.i("STATUS_ERROR", conn.getResponseMessage()); return conn.getInputStream(); }
I tried to add lines
conn.setFixedLengthStreamingMode(contentLength) conn.setChunkedStreamingMode(8 * 1024);
However, the server response is incorrect.
UPDATE:
I could detect a problem. For some reason, when I try to send large bodys to a request, the server creates a timeout , but not with all networks , only with some networks. I use SSL secure SSL, maybe this can cause problems?