Android: Sending a byte[] array via Http POST

androidhttp-post

I am able to do a POST of a parameters string. I use the following code:

String parameters = "firstname=john&lastname=doe";
URL url = new URL("http://www.mywebsite.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(parameters);
out.flush();
out.close();
connection.disconnect();

However, I need to do a POST of binary data (which is in form of byte[]).

Not sure how to change the above code to implement it.
Could anyone please help me with this?

Best Solution

Take a look here Sending POST data in Android

But use ByteArrayEntity.

byte[] content = ...
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(content));           
HttpResponse response = httpClient.execute(httpPost);
Related Question