如何在HttpClient.PostAsync请求中传递长字符串 [英] How to pass long string in HttpClient.PostAsync request
问题描述
尝试将相当长的字符串发送到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?
- 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 + + Uri +字符串+是+太+长+ HttpClient
- .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
推荐答案
答案很简单 - 只需将其放入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请求中传递长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!