如何在HttpClient.PostAsync请求中传递长字符串

时间:2016-01-27 20:08:11

标签: c# http-post httpclient

尝试将相当长的字符串发送到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的已更改文件集。这可能真的很长,所以我真的没办法解决这个问题。有没有办法在没有字符串长度限制的情况下发布内容?

阅读以下主题,但没有在那里找到答案:

2 个答案:

答案 0 :(得分:2)

答案很简单 - 只需将其放入Body中,而不是尝试通过URL推送所有数据

但是,随着票证上的工作显示 - 答案就在How to set large string inside HttpContent when using HttpClient?

FormUrlEncodedContent

中的实际问题

答案 1 :(得分:0)

试试这个......对你有帮助..

 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();