【发布时间】:2016-01-27 20:08:10
【问题描述】:
尝试向 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
【问题讨论】:
-
如果您只在消息中发布 1 个字符,行为是否相同?
-
可能是您的 Youtrack api 对“评论”字段有限制。他们的 API 文档对此有何评论?如果对评论属性/字段有限制-那么-除了缩短它之外,您无能为力...
-
根据他们的文档:没有限制。异常发生在“var content = new FormUrlEncodedContent(postData);”行上
-
@Marty 可以很好地处理 1 个字符,或者 2000 字符。只有 65000 多条消息出错span>
-
这可能是
FormUrlEncodedContent类的问题吗?看看这个答案stackoverflow.com/a/23740338/189738
标签: c# http-post httpclient