title: POST with Basic Auth fails on Android but works in C#
body: <p>I have an app I am developing that requires me to post data to a 3rd party API. I have been struggling with authentication since the beginning and kept putting off further and further, and now I'm stuck.</p>
  
  <p>I have tried using an <code>Authenticator</code>, but have read all about how there appears to be a bug in certain Android versions: <a href="https://stackoverflow.com/questions/1968416/how-to-do-http-authentication-in-android/1968873#1968873" title="Authentication Example">Authentication Example</a></p>
  
  <p>I have tried several different options, including the <code>Apache Commons HTTP Library</code> with no success. After all of this, I decided to make sure that the API wasn't the pain point.  So I wrote a quick <code>WinForms</code> program to test the API, which worked perfectly on the first try.  So, the idea that I'm working from and the API I working with both seem fine, but I am in desperate need of some guidance as to why the Java code isn't working.</p>
  
  <p><strong>Examples follow:</strong></p>
  
  <p><em>C# Code that works everytime:</em></p>
  
  <pre><code>        System.Net.ServicePointManager.Expect100Continue = false;
          // Create a request using a URL that can receive a post. 
          WebRequest request = WebRequest.Create(addWorkoutUrl);
          // Set the Method property of the request to POST.
          request.Method = "POST";
          // Create POST data and convert it to a byte array.
          string postData = "distance=4000&amp;hours=0&amp;minutes=20&amp;seconds=0&amp;tenths=0&amp;month=08&amp;day=01&amp;year=2011&amp;typeOfWorkout=standard&amp;weightClass=H&amp;age=28";
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
          // Set the ContentType property of the WebRequest.
          request.Headers["X-API-KEY"] = apiKey;
          request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("username:password"));
          request.ContentType = "application/x-www-form-urlencoded";
          // Set the ContentLength property of the WebRequest.
          request.ContentLength = byteArray.Length;
          // Get the request stream.
          Stream dataStream = request.GetRequestStream();
          // Write the data to the request stream.
          dataStream.Write(byteArray, 0, byteArray.Length);
          // Close the Stream object.
          dataStream.Close();
          // Get the response.
          WebResponse response = request.GetResponse();
          // Display the status.
          MessageBox.Show(((HttpWebResponse)response).StatusDescription);
          // Get the stream containing content returned by the server.
          dataStream = response.GetResponseStream();
          // Open the stream using a StreamReader for easy access.
          StreamReader reader = new StreamReader(dataStream);
          // Read the content.
          string responseFromServer = reader.ReadToEnd();
          // Display the content.
          MessageBox.Show(responseFromServer);
          // Clean up the streams.
          reader.Close();
          dataStream.Close();
          response.Close();
  </code></pre>
  
  <p><em>Java code for Android that currently returns a 500:Internal Server Error, though I believe this is my fault.</em></p>
  
  <pre><code>    URL url;
      String data = "distance=4000&amp;hours=0&amp;minutes=20&amp;seconds=0&amp;tenths=0&amp;month=08&amp;day=01&amp;year=2011&amp;typeOfWorkout=standard&amp;weightClass=H&amp;age=28";
  HttpURLConnection connection = null;
      //Create connection
      url = new URL(urlBasePath);
      connection = (HttpURLConnection)url.openConnection();
      connection.setConnectTimeout(10000);
      connection.setUseCaches(false);
  connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
      connection.setRequestProperty("Accept","*/*");
      connection.setRequestProperty("X-API-KEY", apiKey);
      connection.setRequestProperty("Authorization", "Basic " +
      Base64.encode((username + ":" + password).getBytes("UTF-8"), Base64.DEFAULT));
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes("UTF-8").length));
  
      DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
      wr.write(data.getBytes("UTF-8"));
      wr.flush();
      wr.close();
      statusCode = connection.getResponseCode();
      statusReason = connection.getResponseMessage();
      //At this point, I have the 500 error...
  </code></pre>