title: HTTP POST to Azure webrole doesn't work from Android app
body: <p>I have an Azure Webrole implemented with WCF REST service.
  I am attempting to upload a small image file (less than 50KB) to this service with an HTTP POST request.</p>
  
  <p>It works in the following methods:</p>
  
  <ol>
  <li>Upload from a windows form application using <code>WebRequest</code>.</li>
  <li>Upload from Android phone when passing through Fiddler proxy.</li>
  </ol>
  
  <p>It fails with the same Android code on the same Android phone when trying to POST directly to the Azure Webrole.</p>
  
  <p>In this case I get a timeout with the <code>SocketException</code> and detailMessage="Connection reset by peer".
  When looking in the VM instance (using RDP) in Azure I see status 400 for this call.</p>
  
  <p>Here is the Android code:</p>
  
  <pre><code>DefaultHttpClient httpclient = new DefaultHttpClient();  
  String requestStr = "http://servername.cloudapp.net/Rest/RestAPI?action_type=upload_file&amp;user_id=5";
  
  HttpPost httpPost = new HttpPost(requestStr);
  HttpParams httpParams = new BasicHttpParams();
  
  // Set the default connection timeout  
  // in milliseconds which is the timeout for waiting for a connection.
  int timeoutConnection = 60000; 
  HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
  
  // Set the default socket timeout (SO_TIMEOUT)  
  // in milliseconds which is the timeout for waiting for data. 
  int timeoutSocket = 60000;
  HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
  
  httpPost.setParams(httpParams);
  httpPost.addHeader("Content-Type", "application/x-data-png");
  httpPost.addHeader("FileName", fileName);
  
  ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bytes);
  httpPost.setEntity(byteArrayEntity);
  
  //Execute HTTP Post Request  
  HttpResponse response = httpclient.execute(httpPost);
  int statusCode = response.getStatusLine().getStatusCode();
  
  HttpEntity entity = response.getEntity();
  String responseStr = EntityUtils.toString(entity);
  </code></pre>
  
  <p>Here is my WebRole code (that I use for testing this issue):</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(UriTemplate = "?action_type=upload_file&amp;user_id={userID}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
  public void UploadFile(string userID, Stream fileContents)
  {
      WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
  }
  </code></pre>
  
  <p>Some more information:
  I tried the same method but sent a short array of bytes like this and it did work:</p>
  
  <pre><code>String someString = "some string";
  byte[] byteArray = someString.getBytes();
  </code></pre>
  
  <p>Which made me think that perhaps I am not reading the file correctly or maybe the byte array is too long (should 50KB be a problem?)</p>
  
  <p>Here is the Android code I use to get the file's bytes:</p>
  
  <pre><code>FileInputStream fileStream = new FileInputStream(fileToUpload);
  fileStream.read(bytes);
  fileStream.close();
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE:</strong> I tried the exact same code in the Android emulator posting directly to the server and it worked... Why isn't this working from a phone?!</p>
  
  <p><strong>UPDATE:</strong> Turns out it was a weird problem with the WiFi on my router(!)</p>
  
  <p>Everything else worked fine and only upload from an Android phone to Azure above a certain size failed... (Other uploads from the same phone did work...)
  I changed my router to a new one and everything is working again.</p>
  
  <p>Also: I would like to commend the Azure support person who did not abandon me for a whole week and helped me finally understand the problem.</p>
  
  <p>I guess I should treat this as a freak accident and go on with my life :-)</p>
  
  <p>It also means the code above is a good example of how to do things. Hope it will help someone...</p>