How to pass long string in HttpClient.PostAsync request

不想你离开。 提交于 2019-12-11 10:49:31

问题


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

Invalid URI: The Uri string is too long.

My code:

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);

So I took my chances with a 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);

Which results in the exact same issue.

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. 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:


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

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

The actual problem beeing in the FormUrlEncodedContent




回答2:


Try this..Will be helpful for 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();


来源:https://stackoverflow.com/questions/35047048/how-to-pass-long-string-in-httpclient-postasync-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!