尝试将相当长的字符串发送到 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 时更改的文件集。这可能会很长,所以我真的没有办法解决这个问题。有没有办法在没有字符串长度限制的情况下发布内容?
阅读以下主题,但没有找到答案:
- .NET HttpClient. How to POST string value?
- How do I set up HttpContent for my HttpClient PostAsync second parameter?
- https://psycodedeveloper.wordpress.com/2014/06/30/how-to-call-httpclient-postasync-with-a-query-string/
- http://forums.asp.net/t/2057125.aspx?Invalid+URI+The+Uri+string+is+too+long+HttpClient
最佳答案
简单的回答 - 只需将其放入正文中,而不是尝试通过 URL 推送所有数据
但正如票证上的工作所示 - 答案就在这里 How to set large string inside HttpContent when using HttpClient?
实际问题出现在 FormUrlEncodedContent
关于c# - 如何在 HttpClient.PostAsync 请求中传递长字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047048/