FormUrlEncodedContent的大小限制解决方法 [英] size limit workaround for FormUrlEncodedContent

查看:857
本文介绍了FormUrlEncodedContent的大小限制解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:

System.UriFormatException: Invalid URI: The Uri string is too long.

问题在于此行:

FormUrlEncodedContent content = new FormUrlEncodedContent(postData); 

对此进行研究后,我了解到这是由于 FormUrlEncodedContent类的大小限制。但是我不确定该如何解决?参见以下代码:

Upon researching this I've learned it is because of a size limitation of the class FormUrlEncodedContent. But I'm not sure how I can workaround this? See code below:

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

有人可以帮忙吗?

推荐答案

如果您的请求较大,请改用multipart / form-data:

If your request is larger use multipart/form-data instead:

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

这篇关于FormUrlEncodedContent的大小限制解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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