【问题标题】:How to pass long string in HttpClient.PostAsync request如何在 HttpClient.PostAsync 请求中传递长字符串
【发布时间】: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 的更改文件集。这可能真的很长,所以我真的没有办法解决这个问题。有没有办法在没有字符串长度限制的情况下发布内容?

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

【问题讨论】:

  • 如果您只在消息中发布 1 个字符,行为是否相同?
  • 可能是您的 Youtrack api 对“评论”字段有限制。他们的 API 文档对此有何评论?如果对评论属性/字段有限制-那么-除了缩短它之外,您无能为力...
  • 根据他们的文档:没有限制。异常发生在“var content = new FormUrlEncodedContent(postData);”行上
  • @Marty 可以很好地处理 1 个字符,或者 2000 字符。只有 65000 多条消息出错​​span>
  • 这可能是FormUrlEncodedContent 类的问题吗?看看这个答案stackoverflow.com/a/23740338/189738

标签: c# http-post httpclient


【解决方案1】:

简短的回答 - 只需将其放入正文中,而不是尝试通过 URL 推送所有数据

但正如票证上的工作所显示的那样 - 答案就在这里How to set large string inside HttpContent when using HttpClient?

FormUrlEncodedContent中的实际问题

【讨论】:

  • 第二个例子就是这样做的,不是吗?
【解决方案2】:

试试这个..将对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();

【讨论】:

  • 我正在做这个键值操作...但 uri 仍然太长...
猜你喜欢
  • 2019-11-18
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多