簡體   English   中英

如何在HttpClient.PostAsync請求中傳遞長字符串

[英]How to pass long string in HttpClient.PostAsync request

嘗試將較長的字符串發送到REST Web API(youtrack)。 我得到以下異常:

無效的URI:Uri字符串太長。

我的代碼:

var encodedMessage = HttpUtility.UrlEncode(message);
var requestUri = string.Format("{0}{1}issue/{2}/execute?comment={3}", url, YoutrackRestUrl, issue.Id, encodedMessage);
var response = await httpClient.PostAsync(requestUri, null).ConfigureAwait(false);

所以我抓住了一個FormUrlEncodedContent

var requestUri = string.Format("{0}{1}issue/{2}/execute", url, YoutrackRestUrl, issue.Id);

var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("comment", message));

var content = new FormUrlEncodedContent(postData);
var response = await httpClient.PostAsync(requestUri, content).ConfigureAwait(false);

導致完全相同的問題。

我要發送的字符串(注釋)是更改為SVN的提交的文件集。 這可能真的很長,所以我真的沒有辦法解決這個問題。 有沒有一種不受字符串長度限制的發布內容的方法?

閱讀以下主題,但找不到答案:

簡短的答案-只需將其放入正文中,而不是嘗試通過URL推送所有數據

但是如票證上的工作所示-答案是在這里使用HttpClient時如何在HttpContent中設置大字符串?

實際的問題在FormUrlEncodedContent

試試這個。.將對uwp有所幫助。

 Uri uri = new Uri("your uri string");
 Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
 var value1 = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,string>>
 { 
     // your key value pairs
 };

 var response = await client.PostAsync(uri,new HttpFormUrlEncodedContent(value1));
 var result = await response.Content.ReadAsStringAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM