title: Posting Audio (byte array) to MVC.NET Web Api
body: <h1>The Problem</h1>
  
  <p>I am trying to send audio recorded by my android device to a MVC.NET Web Api.  I confirmed the connection by passing simple string parameters.  However, when I try to pass the byte array generated from the audio, I get a 500 from the server every time.  I have tried multiple configurations, but here's what I currently have:</p>
  
  <h1>MVC Web API</h1>
  
  <pre><code>public class PostParms
  {
      public string AudioSource { get; set; }
      public string ExtraInfo { get; set; }
  }
  
  public class MediaController : ApiController
  {
      [HttpPost]
      public string Post([FromBody]PostParms parms)
      {
          byte[] bytes = System.Text.Encoding.UTF8.GetBytes(parms.AudioSource);
          return "success";
      }
  }
  </code></pre>
  
  <h1>Android Code</h1>
  
  <pre><code>public class WebServiceTask extends AsyncTask&lt;String, Void, Long&gt;
  {   
      @Override
      protected Long doInBackground(String... parms)
      {
          long totalsize = 0;
          String filepath = parms[0];
          byte[] fileByte = convertAudioFileToByte(new File(filepath));        
  
          //Tried base64 below with Convert.FromBase64 on the C# side
          //String bytesstring = Base64.encodeToString(fileByte, Base64.DEFAULT);
  
          String bytesstring = "";
          try 
          {
              String t = new String(fileByte,"UTF-8");
      bytesstring = t;
          } catch (UnsupportedEncodingException e1)
          {
          // TODO Auto-generated catch block
          e1.printStackTrace();
          }
  
          URL url;
          HttpURLConnection urlConnection = null;
          try {
              url = new URL("http://my.webservice.com:8185/api/media");
  
              //setup for connection
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setUseCaches(false);
              urlConnection.setRequestMethod("POST");
              urlConnection.setDoOutput(true);
              urlConnection.setRequestProperty("Content-Type","application/json");
              urlConnection.setRequestProperty("Accept","application/json");
              urlConnection.connect();
  
              JSONObject json = new JSONObject();
              json.put("AudioSource", bytesstring);
              json.put("ExtraInfo", "none");
  
              DataOutputStream output = new DataOutputStream(urlConnection.getOutputStream());
              output.writeBytes(URLEncoder.encode(json.toString(),"UTF-8"));
              output.flush();
              output.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          finally {
              urlConnection.disconnect();
          }
          return totalsize;
      }
  
      protected void onPreExecute(){
  
      }
      protected void onPostExecute(){
  
      }
      private byte[] convertAudioFileToByte(File file) {
          byte[] bFile = new byte[(int) file.length()];
  
          try {
              // convert file into array of bytes
              FileInputStream fileInputStream = new FileInputStream(file);
              fileInputStream.read(bFile);
              fileInputStream.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return bFile;
      }
  }
  </code></pre>
  
  <p>I can get a .NET application to work with the same API sending an audio stream, but not Android.  Like I said, I've tried a number of configurations found on the internet with encoding and the output stream, but continue to get 500.  I need help, as I am at a loss.</p>
  
  <p>Thanks in advance</p>