2014-05-16 63 views
23

因此,我創建了一個HttpClient,並使用HttpClient.PostAsync()發佈數據。如何在使用HttpClient時在HttpContent中設置大字符串?

我設置使用

HttpContent content = new FormUrlEncodedContent(post_parameters)HttpContent;其中post_parameters是鍵值對List<KeyValuePair<string, string>>

問題是一個列表,當HttpContent有一個較大的值(圖像轉換爲base64要發送的),我得到一個網址太長錯誤。這是有道理的 - 導致url不能超過32,000個字符。但是,如果不是這樣,我如何將數據添加到HttpContent

請幫忙。

+1

Post'StreamContent' –

+0

我很困惑......當你使用任何類型的'HttpContent'(包括'FormUrlEncodedContent')時,數據在主體中,而不在Url ..所以你爲什麼要在Url中看到數據? –

+0

當我撥打以下電話時, dict.Add(new KeyValuePair (「Base64FileData」,image)); (其中image是base 64轉換後的字符串)。拋出異常。 – Muraad

回答

33

我在朋友的幫助下計算出來的。你想要做的就是避免使用FormUrlEncodedContent(),因爲它對uri的大小有限制。相反,你可以做到以下幾點:

var jsonString = JsonConvert.SerializeObject(post_parameters); 
    var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); 

在這裏,我們並不需要使用HttpContent張貼到服務器,的StringContent幹得不錯!

+3

>在這裏,我們不需要使用HttpContent發佈到服務器,StringContent可以完成工作! 'StringContent'實際上是'HttpContent'(它是從類似於'FormUrlEncodedContent'的抽象'HttpContent'派生的) – eXavier

+0

這是迄今爲止最簡單的方法。我在2016年初看到的其他任何東西都是PITA。 – mafu

23

FormUrlEncodedContent內部使用Uri.EscapeDataString:從反射,我可以看到,這種方法有限制請求長度的常量。

可能的解決方案是通過使用System.Net.WebUtility.UrlEncode(.net 4.5)來繞過此限制創建FormUrlEncodedContent的新實現。

public class MyFormUrlEncodedContent : ByteArrayContent 
{ 
    public MyFormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection) 
     : base(MyFormUrlEncodedContent.GetContentByteArray(nameValueCollection)) 
    { 
     base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); 
    } 
    private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, string>> nameValueCollection) 
    { 
     if (nameValueCollection == null) 
     { 
      throw new ArgumentNullException("nameValueCollection"); 
     } 
     StringBuilder stringBuilder = new StringBuilder(); 
     foreach (KeyValuePair<string, string> current in nameValueCollection) 
     { 
      if (stringBuilder.Length > 0) 
      { 
       stringBuilder.Append('&'); 
      } 

      stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Key)); 
      stringBuilder.Append('='); 
      stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Value)); 
     } 
     return Encoding.Default.GetBytes(stringBuilder.ToString()); 
    } 
    private static string Encode(string data) 
    { 
     if (string.IsNullOrEmpty(data)) 
     { 
      return string.Empty; 
     } 
     return System.Net.WebUtility.UrlEncode(data).Replace("%20", "+"); 
    } 
} 

要發送大內容,最好使用StreamContent

+0

我不能使用這個解決方案,因爲我使用.Net 4.0,我沒有訪問方法UrlEncode()。由於工作限制,我無法升級任何其他想法? – Muraad

+0

通過添加一個System.Web依賴項,你可以找到一個類似的方法HttpUtility.UrlEncode – Cybermaxs

+0

你給我的實現和FormUrlEcondedContent()具有相同的限制,但是現在不用拋出異常就可以跳過發送到服務器的調用。 – Muraad

0

此代碼對我的作品,基本上你發送POST數據「應用程序/ x-WWW的形式,進行了urlencoded」字符串內容over HTTP客戶端內,希望這能幫助有同樣問題的人喜歡我

void sendDocument() 
    { 
     string url = "www.mysite.com/page.php"; 
     StringBuilder postData = new StringBuilder(); 
     postData.Append(String.Format("{0}={1}&", HttpUtility.HtmlEncode("prop"), HttpUtility.HtmlEncode("value"))); 
     postData.Append(String.Format("{0}={1}", HttpUtility.HtmlEncode("prop2"), HttpUtility.HtmlEncode("value2"))); 
     StringContent myStringContent = new StringContent(postData.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded"); 
     HttpClient client = new HttpClient(); 
     HttpResponseMessage message = client.PostAsync(url, myStringContent).GetAwaiter().GetResult(); 
     string responseContent = message.Content.ReadAsStringAsync().GetAwaiter().GetResult(); 
    } 
+0

但是OP的問題是發佈大量的數據,你沒有證明可以用這種方式完成。然而,我看到你在做什麼 - 使用'application/x-www-form-urlencoded',但繞過使用'類FormUrlEncodedContent'似乎有內容長度限制。 – HankCa

相關問題