How to pass long string in HttpClient.PostAsync re

2019-08-13 03:06发布

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:

2条回答
ゆ 、 Hurt°
2楼-- · 2019-08-13 03:48

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

查看更多
姐就是有狂的资本
3楼-- · 2019-08-13 04:08

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();
查看更多
登录 后发表回答