繁体   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