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

编程入门 行业动态 更新时间:2024-10-25 22:33:56
本文介绍了如何在HttpClient.PostAsync请求中传递长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试将相当长的字符串发送到REST Web api(youtrack)。我得到以下异常:

Trying to send a rather long string to a REST web api (youtrack). I get the following exception:

无效的URI:Uri字符串太长。

Invalid URI: The Uri string is too long.

我的代码:

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

The string (comment) I am sending, is the changed file set of a commit into SVN. Which can be really long, so I don't really have a way to get around that. Is there a way to post content without the string length restriction?

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

Read the following topics, but didn't find an answer there:

  • .NET HttpClient。如何发布字符串值?
  • 如何为我的HttpClient PostAsync第二个参数设置HttpContent?
  • psycodedeveloper.wordpress/2014/ 06/30 / how-to-call-httpclient-postasync-with-a-query-string /
  • forums.asp/t/2057125.aspx?Invalid + URI + + Uri +字符串+是+太+长+ HttpClient
  • .NET HttpClient. How to POST string value?
  • How do I set up HttpContent for my HttpClient PostAsync second parameter?
  • psycodedeveloper.wordpress/2014/06/30/how-to-call-httpclient-postasync-with-a-query-string/
  • forums.asp/t/2057125.aspx?Invalid+URI+The+Uri+string+is+too+long+HttpClient
推荐答案

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

Well the short answer to it - just put it into the Body, instead of trying to push all the data via the URL

但是作为票证上的工作显示 - 答案在这里如何在使用HttpClient时在HttpContent中设置大字符串?

But as the work on the ticket showed - the answer was here How to set large string inside HttpContent when using HttpClient?

FormUrlEncodedContent中的实际问题

更多推荐

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

本文发布于:2023-11-08 22:05:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   HttpClient   PostAsync

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!