title: Sending multipart form in android by HttpURLConnection (POST)
body: <p>I want to send multipart form in my android app but without using org.apache.http.entity.mime so I've created mu own way but it doesn't work the way how I'm creating request:</p>
  
  <pre><code>    public byte[] createRequest(byte[] imagedata){
      byte[] requestData = null;
      ByteArrayOutputStream buf = new ByteArrayOutputStream();
      OutputStreamWriter output = null;
      try {
          Log.i(TAG, "Creating request");
          output = new OutputStreamWriter(buf, "UTF-8");
          output.write("--");
          output.write(boundary);
          output.write("\r\n");
          output.write("Content-Disposition: form-data; name=\"auth\"; filename=\"auth\"\r\n");
          output.write("Content-Type: text/xml; charset=utf-8\r\n");
          output.write("\r\n");
  
          byte temp2[] = buf.toByteArray();
          Log.i(TAG, "BUF SIZE: " + temp2.length);
  
          Log.i(TAG, "BUF: " + buf.toString());
          ByteArrayOutputStream authBuffer = new ByteArrayOutputStream();
          OutputStreamWriter authOut = new OutputStreamWriter(authBuffer, "UTF-8");
          writeAuthRequestFragment(user, pass, company_id, "RESLINK CLIENT", "2.0", null, null, null, authOut);
  
          buf.write(authBuffer.toByteArray());
  
          Log.i(TAG, "BUF SIZE: " + buf.size());
  
          output.write("\r\n--" + boundary + "\r\n");
          output.write("Content-Disposition: form-data; name=\""+CIMAGE+"\"; filename=\""+CIMAGE+"\"\r\n");
          output.write("Content-Type: "+IMAGE_PNG+"\r\n");
          output.write("\r\n");
          buf.write(imagedata);
          output.write("\r\n--" + boundary + "--\r\n");
          requestData = buf.toByteArray();
          Log.i(TAG, "WHOLE SIZE " + requestData.length);
          output.flush();
      } catch(IOException ex){
          ex.printStackTrace();
      }
      finally{
          if(buf!=null){
              try {
                  buf.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          if(output!=null){
              try {
                  output.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return requestData;
  }
  </code></pre>
  
  <p>When the method is called the log information are showing that ByteArrayOutputStream have size 0 to the moment when I'm adding imagedata so it look like the OutputStreamWriter doesn't write strings, or am I wrong?</p>
  
  <p>Any suggestions or solutions?</p>