I am developing an Android app and have already found out that different Android versions have a different ways on how they handle Http(s)URLConnections (http://stackoverflow.com/q/9556316/151682).
I ran into the issue that Android 4 performs a POST request over HTTPS nicely, adding headers like Content-Type automatically when running the code below.
However, on Android 2.3.5 (device & emulator), any write to the output stream seems to be ignored - I debugged it with the web proxy Charles and while all headers are sent, the data written to the output stream is not sent along...
Anyone knows how to solve this?
Note: As the API I am developing against has only a self-signed certificate, I need to disable certification validation at this time.
TIA, Patrick
Update In the meantime, I also have tried to following, to no avail:
- Calling
close()
after theflush()
call on the BufferedOutputStream - Calling
close()
on both the OutputStream and the BufferedOutputStream - Using a OutputStreamWriter instead
Not calling
close()
before callinggetInputStream()
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setDoOutput(true); // Triggers POST. connection.setRequestMethod("POST"); int contentLength = 0; if(body != null) { contentLength = body.getBytes().length; } // Workarounds for older Android versions who do not do that automatically (2.3.5 for example) connection.setRequestProperty(HTTP.TARGET_HOST, url.getHost()); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Set SSL Context -- Development only if(context != null && connection instanceof HttpsURLConnection){ HttpsURLConnection conn = (HttpsURLConnection)connection; conn.setSSLSocketFactory(context.getSocketFactory()); conn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } try{ // Add headers if(headers != null){ for (NameValuePair nvp : headers) { if(nvp != null){ connection.setRequestProperty(nvp.getName(), nvp.getValue()); } } } connection.setFixedLengthStreamingMode(contentLength); OutputStream outputStream = null; try { if(body != null){ outputStream = connection.getOutputStream(); BufferedOutputStream stream = new BufferedOutputStream(outputStream); stream.write(body.getBytes()); // <<<< No effect ?! stream.flush(); } } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException logOrIgnore) { // ... } } InputStream inputStream = connection.getInputStream(); // .... Normal case .... } catch(IOException e){ // ... Exception! Check Error stream and the response code ... } finally{ connection.disconnect(); }
}