2014-05-07 18 views
9

我試圖發送一些圖像+一些元數據到服務器通過HTTP發佈從Windows應用商店,但試圖實際包括在帖子中的數據時卡住了。由於商店應用程序API的更改,無法按照在Windows窗體應用程序或類似應用程序中完成此操作的方式完成。發送字節數組通過HTTP POST在商店應用程序

我得到的錯誤。

cannot convert source type byte[] to target type System.Net.Http.httpContent 

現在這顯然是因爲它是不能隱鑄造2種不同類型,但它基本上就是我希望能夠做到。我如何得到我的字節數組數據到httpContent類型,以便我可以將其包含在以下調用

httpClient.PostAsync(Uri uri,HttpContent content); 

這裏是我的全部上傳方法:

async private Task UploadPhotos(List<Photo> photoCollection, string recipient, string format) 
    { 
     PhotoDataGroupDTO photoGroupDTO = PhotoSessionMapper.Map(photoCollection); 

     try 
     { 
      var client = new HttpClient(); 
      client.MaxResponseContentBufferSize = 256000; 
      client.DefaultRequestHeaders.Add("Upload", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); 

      // POST action_begin 
      const string actionBeginUri = "http://localhost:51139/PhotoService.axd?action=Begin"; 
      HttpResponseMessage response = await client.GetAsync(actionBeginUri); 
      response.EnsureSuccessStatusCode(); 
      string responseBodyAsText = await response.Content.ReadAsStringAsync(); 
      string id = responseBodyAsText; 
      //// 

      // POST action_upload 
      Uri actionUploadUri = new Uri("http://localhost:51139/PhotoService.axd?action=Upload&brand={0}&id={1}&name={2}.jpg"); 

      var metaData = new Dictionary<string, string>() 
      { 
       {"Id", id}, 
       {"Brand", "M3rror"}, //TODO: Denne tekst skal komme fra en konfigurationsfil. 
       {"Format", format}, 
       {"Recipient", recipient} 
      }; 

      string stringData = ""; 
      foreach (string key in metaData.Keys) 
      { 
       string value; 
       metaData.TryGetValue(key, out value); 
       stringData += key + "=" + value + ","; 
      } 

      UTF8Encoding encoding = new UTF8Encoding(); 
      byte[] byteData = encoding.GetBytes(stringData); 

      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, actionUploadUri); 

      // send meta data 
      // TODO get byte data in as content 
      HttpContent metaDataContent = byteData; 
      HttpResponseMessage actionUploadResponse = await client.PostAsync(actionUploadUri, metaDataContent); 

      actionUploadResponse.EnsureSuccessStatusCode(); 
      responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync(); 

      // send photos 
      // TODO get byte data in as content 
      foreach (byte[] imageData in photoGroupDTO.PhotosData) 
      { 
       HttpContent imageContent = imageData; 
       actionUploadResponse = await client.PostAsync(actionUploadUri, imageContent); 
       actionUploadResponse.EnsureSuccessStatusCode(); 
       responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync(); 
      }     
      //// 

      // POST action_complete 
      const string actionCompleteUri = "http://localhost:51139/PhotoService.axd?action=Complete"; 
      HttpResponseMessage actionCompleteResponse = await client.GetAsync(actionCompleteUri); 
      actionCompleteResponse.EnsureSuccessStatusCode(); 
      responseBodyAsText = await actionCompleteResponse.Content.ReadAsStringAsync(); 
      //// 
     } 

     catch (HttpRequestException e) 
     { 
     } 
     catch (Exception e) 
     { 
      Debug.WriteLine(e.ToString()); 
     } 
    } 
+0

隱而不宣二進制數據需要*序列化*?你也許想要*流*它,而不是一次全部上傳。 http://stackoverflow.com/questions/19005991/webapi-how-to-handle-images可能有所幫助。 – bytefire

回答

18

這將是更直接地使用System.Net.Http.ByteArrayContent。例如:

// Converting byte[] into System.Net.Http.HttpContent. 
byte[] data = new byte[] { 1, 2, 3, 4, 5}; 
ByteArrayContent byteContent = new ByteArrayContent(data); 
HttpResponseMessage reponse = await client.PostAsync(uri, byteContent); 

對於只能用特定的文本編碼使用文本:

// Convert string into System.Net.Http.HttpContent using UTF-8 encoding. 
StringContent stringContent = new StringContent(
    "blah blah", 
    System.Text.Encoding.UTF8); 
HttpResponseMessage reponse = await client.PostAsync(uri, stringContent); 

或者像您以上使用的multipart/form-data的所提到的,文本和圖像的:

// Send binary data and string data in a single request. 
MultipartFormDataContent multipartContent = new MultipartFormDataContent(); 
multipartContent.Add(byteContent); 
multipartContent.Add(stringContent); 
HttpResponseMessage reponse = await client.PostAsync(uri, multipartContent); 
+0

哪個足夠快速,安全地處理包含文本和數字組合的數據(在list-json序列化中的大約1lakh記錄中)? ByteArrayContent或StringContent。請建議 – Ganesh

+0

'StringContent'應該足夠好 – kiewic

+0

謝謝:) @kiewic – Ganesh

0

你正在尋找這個概念被稱爲Serialization。序列化意味着準備您的數據(可能是異構的,沒有預定義的結構)用於存儲或傳輸。然後,當您需要再次使用數據時,執行相反的操作,反序列化,並取回原始數據結構。上面的鏈接顯示了一些關於如何在C#中完成的方法。

+0

上面的鏈接不顯示任何東西 –

7

您需要將字節數組包裝在HttpContent類型中。

如果您正在使用系統,Net.Http.HttpClient:

HttpContent metaDataContent = new ByteArrayContent(byteData); 

如果您使用的是首選Windows.Web.Http.HttpClient:

Stream stream = new MemoryStream(byteData); 
HttpContent metaDataContent = new HttpStreamContent(stream.AsInputStream()); 
+1

你剛剛救了我的命! –

+0

接受「ByteArrayContent」的服務器端操作模式應該是什麼? – Shimmy

相關問題