C# FormUrlEncodedContent的大小限制解决方法

C# FormUrlEncodedContent的大小限制解决方法,c#,.net,C#,.net,我收到错误信息: System.UriFormatException: Invalid URI: The Uri string is too long. 这一行的问题在于: FormUrlEncodedContent content = new FormUrlEncodedContent(postData); 通过研究,我了解到这是因为类的大小限制FormUrlEncodedContent。但我不知道我该怎么解决这个问题?见下面的代码: public Token RequestToken

我收到错误信息:

System.UriFormatException: Invalid URI: The Uri string is too long.
这一行的问题在于:

FormUrlEncodedContent content = new FormUrlEncodedContent(postData); 
通过研究,我了解到这是因为类的大小限制FormUrlEncodedContent。但我不知道我该怎么解决这个问题?见下面的代码:

 public Token RequestToken(string username, int businessID, string requestXml)
    {
        var postData = new Dictionary<string, string>() { { "username", username }, { "businessID", businessID.ToString() }, { "authenticator", requestXml } };
        FormUrlEncodedContent content = new FormUrlEncodedContent(postData);          

        try
        {
            HttpResponseMessage response = _client.PostAsync("Token", content).Result;
            if (response.IsSuccessStatusCode)
            {
                return response.Content.ReadAsAsync<Token>().Result;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
        }

        return null;
    }
public-Token-RequestToken(字符串用户名、int-businessID、字符串requestXml)
{
var postData=newdictionary(){{“username”,username},{“businessID”,businessID.ToString()},{“authenticator”,requestXml};
FormUrlEncodedContent内容=新的FormUrlEncodedContent(postData);
尝试
{
HttpResponseMessage响应=_client.PostAsync(“令牌”,内容)。结果;
if(响应。IsSuccessStatusCode)
{
返回response.Content.ReadAsAsync().Result;
}
}
捕获(例外情况除外)
{
日志错误(ex);
}
返回null;
}

有人能帮上忙吗?

让我们调整现有代码以适应中的解决方案

int-limit=2000;
StringContent=new StringContent(postData.Aggregate(new StringBuilder(),(sb,nxt)=>{
StringBuilder sbInternal=新建StringBuilder();
如果(某人长度>0)
{
某人附加(“&”);
}
int循环=nxt.Value.Length/limit;

对于(int i=0;i如果您的请求较大,请使用多部分/表单数据:

using (var content = new MultipartFormDataContent())
{
    foreach (var keyValuePair in data)
    {
        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
    }

    // send POST request
    using (var client = new HttpClient())
    {
        return client.PostAsync(identifier.IsirUrl + uri, content).GetAwaiter().GetResult();
    }
}

看一看,我对提供的链接中的示例有点困惑。该示例使用字符串,我有一本字典?
using (var content = new MultipartFormDataContent())
{
    foreach (var keyValuePair in data)
    {
        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
    }

    // send POST request
    using (var client = new HttpClient())
    {
        return client.PostAsync(identifier.IsirUrl + uri, content).GetAwaiter().GetResult();
    }
}