Question: Since the Android developers [recommend](http://android-developers.blogspot.nl/2011/09/androids-http-clients.html) to use the `HttpURLConnection` class, I was wondering if anyone can provide me with a good example on how to send a bitmap "file" (actually an in-memory stream) via POST to an Apache HTTP server. I'm not interested in cookies or authentication or anything complicated, but I just want to have a reliable and logic implementation. All the examples that I've seen around here look more like "let's try this and maybe it works".

Right now, I have this code:

```
URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL("http://example.com/server.cgi");

    urlConnection = (HttpURLConnection) url.openConnection();

} catch (Exception e) {
    this.showDialog(getApplicationContext(), e.getMessage());
}
finally {
    if (urlConnection != null)
    {
        urlConnection.disconnect();
    }
}

```

where showDialog should just display an `AlertDialog` (in case of an invalid URL?).

Now, let's say that I generate a bitmap like so: `Bitmap image = this.getBitmap()` inside a control derived from `View` and I want to send it via POST. What would be the proper procedure to achieve such a thing? What classes do I need to use? Can I use `HttpPost` like in [this example](https://stackoverflow.com/a/4126746/1174378)? If so, how would I construct the `InputStreamEntity` for my bitmap? I would find it revolting to be required to first store the bitmap in a file on the device.

---

I should also mention that I really need to send every unaltered pixel of the original bitmap to the server, so I can't convert it to JPEG.

Answer: 
I haven't tested this, but you might try using PipedInputStream and PipedOutputStream. It might look something like:

```
final Bitmap bmp = … // your bitmap

// Set up Piped streams
final PipedOutputStream pos = new PipedOutputStream(new ByteArrayOutputStream());
final PipedInputStream pis = new PipedInputStream(pos);

// Send bitmap data to the PipedOutputStream in a separate thread
new Thread() {
    public void run() {
        bmp.compress(Bitmap.CompressFormat.PNG, 100, pos);
    }
}.start();

// Send POST request
try {
    // Construct InputStreamEntity that feeds off of the PipedInputStream
    InputStreamEntity reqEntity = new InputStreamEntity(pis, -1);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true);
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
    e.printStackTrace()
}

```
I tried the solutions above and none worked for me out of the box.

However <http://www.baeldung.com/httpclient-post-http-request>. Line 6 POST Multipart Request worked within seconds

```
public void whenSendMultipartRequestUsingHttpClient_thenCorrect() 
  throws ClientProtocolException, IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://www.example.com");

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addTextBody("username", "John");
    builder.addTextBody("password", "pass");
    builder.addBinaryBody("file", new File("test.txt"),
      ContentType.APPLICATION_OCTET_STREAM, "file.ext");

    HttpEntity multipart = builder.build();
    httpPost.setEntity(multipart);

    CloseableHttpResponse response = client.execute(httpPost);
    client.close();
}

```