如何在HttpClient.PostAsync请求中传递长字符串 [英] How to pass long string in HttpClient.PostAsync request

查看:975
本文介绍了如何在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. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆