title: Cannot upload to azure Blob Storage: The remote server returned an error: (400) Bad Request
body: <p>I'm trying to create a utility to download file from the internet and upload it again to Azure blob storage. 
  Blob containers already created well; But for some reason i'm getting "Bad Request 400" exception when i tried to upload the file to storage ... Container name is created, small letters, so special characters. But I still do not know why i'm getting the exception!</p>
  
  <p>Please help.</p>
  
  <p><strong>Note</strong>: </p>
  
  <ul>
  <li>I'm not using any emulator ... Directly testing on the cloud.</li>
  <li>All of my containers with "Public Container" access option.</li>
  </ul>
  
  <p>Here is the exception:</p>
  
  <pre><code>An exception of type 'Microsoft.WindowsAzure.Storage.StorageException' 
  occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code
  Additional information: The remote server returned an error: (400) Bad Request.
  </code></pre>
  
  <p>And here is the code:</p>
  
  <pre><code>foreach (var obj in objectsList)
  {
       var containerName = obj.id.Replace("\"", "").Replace("_", "").Trim();
       CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
  
       if (blobContainer.Exists())
       {
           var fileNamesArr = obj.fileNames.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
  
           foreach (var sora in fileNamesArr)
           {
               int soraInt = int.Parse(sora.Replace("\"", ""));
               String fileName = String.Format("{0}.mp3", soraInt.ToString("000"));
  
               var url = String.Format("http://{0}/{1}/{2}", obj.hostName.Replace("\"", ""), obj.id.Replace("\"", ""), fileName.Replace("\"", "")).ToLower();
  
               var tempFileName = "temp.mp3";
  
               var downloadedFilePath = Path.Combine(Path.GetTempPath(), tempFileName).ToLower();
  
               var webUtil = new WebUtils(url);
               await webUtil.DownloadAsync(url, downloadedFilePath).ContinueWith(task =&gt;
               {
                   var blobRef = blobContainer.GetBlockBlobReference(fileName.ToLower());
                   blobRef.Properties.ContentType = GetMimeType(downloadedFilePath);
  
                   using (var fs = new FileStream(downloadedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                   {
                       blobRef.UploadFromStream(fs); // &lt;--- Exception
                   }
               });
           }
        }
        else
        {
            throw new Exception(obj.id.Replace("\"", "") + " Container not exist!");
        }
  }
  </code></pre>
  
  <p>Edit: The Storage Exception</p>
  
  <blockquote>
    <p>Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
       at System.Net.HttpWebRequest.GetRequestStream(TransportContext&amp; context)
       at System.Net.HttpWebRequest.GetRequestStream()
       at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand<code>1 cmd, IRetryPolicy policy, OperationContext operationContext)
       --- End of inner exception stack trace ---
       at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand</code>1 cmd, IRetryPolicy policy, OperationContext operationContext)
       at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
       at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
       at TelawatAzureUtility.StorageService.&lt;>c__DisplayClass4.b__12(Task task) in \psf\Home\Documents\Visual Studio 14\Projects\Telawat Azure Utility\TelawatAzureUtility\StorageService.cs:line 128
    Request Information
    RequestID:
    RequestDate:Sat, 28 Jun 2014 20:12:14 GMT
    StatusMessage:Bad Request</p>
  </blockquote>
  
  <p>Edit 2: Request Information:</p>
  
  <p><img src="https://i.stack.imgur.com/rA5Qe.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/Qw12L.png" alt="enter image description here"></p>
  
  <p>Edit 3: The problem comes from WebUtils .. I replaced it with below code and it works! I will add weUtils code, maybe you can help to know what is the problem with it.</p>
  
  <pre><code>HttpClient client = new HttpClient();
  var stream = await client.GetStreamAsync(url);
  </code></pre>
  
  <p>WebUtils Code: </p>
  
  <pre><code>public class WebUtils
  {
      private Lazy&lt;IWebProxy&gt; proxy;
  
      public WebUtils(String url)
      {
          proxy = new Lazy&lt;IWebProxy&gt;(() =&gt; string.IsNullOrEmpty(url) ? null : new WebProxy {
              Address = new Uri(url), UseDefaultCredentials = true });
      }
  
      public IWebProxy Proxy
      {
          get { return proxy.Value; }
      }
  
      public Task DownloadAsync(string requestUri, string filename)
      {
          if (requestUri == null)
              throw new ArgumentNullException("requestUri is missing!");
  
          return DownloadAsync(new Uri(requestUri), filename);
      }
  
      public async Task DownloadAsync(Uri requestUri, string filename)
      {
          if (filename == null)
              throw new ArgumentNullException("filename is missing!");
  
          if (Proxy != null)
          {
              WebRequest.DefaultWebProxy = Proxy;
          }
  
          using (var httpClient = new HttpClient())
          {
              using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
              {
                  using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync())
                  {
                      using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
                      {
                          contentStream.CopyTo(stream);
                          stream.Flush();
                          stream.Close();
                      }
                      contentStream.Close();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Also when I tried this code ... the 'Wait' will never finish or completed!</p>
  
  <pre><code>webUtil.DownloadAsync(url, downloadedFilePath).Wait()
  </code></pre>