无效的 URI:尝试将 post 值字典 API 时 Uri 字符串太长

Invalid URI: The Uri string is too long when trying to post dictionary of values to API

下面的代码正在构造一个 post 对 API 的请求,但在某些情况下 html_text 是巨大的(70,000 个字符+),这似乎太多导致了 Invalid URI: The Uri string is too long 错误,我找不到解决这个问题的方法。

如何增加有效 URI 限制?

public async Task<string> CreateCampaign(string apiKey, string fromName, string fromEmail, string replyTo, string title,
            string subject, string plaintext, string htmltext, string listIds, string segmentIds, string excludeListIds,
            string excludeSegmentIds, string brandId, string queryString, string sendCampaign)
        {
            var values = new Dictionary<string, string>
            {
               { "api_key", apiKey },
               { "from_name", fromName },
               { "from_email", fromEmail },
               { "reply_to", replyTo },
               { "title", title },
               { "subject", subject },
               { "plain_text", plaintext },
               { "html_text", htmltext },
               { "list_ids", listIds },
               { "segment_ids", segmentIds },
               { "exclude_list_ids", excludeListIds },
               { "exclude_segment_ids", excludeSegmentIds },
               { "brand_id", brandId },
               { "query_string", queryString },
               { "send_campaign", sendCampaign },

            };

            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync("[Redacted-DOMAIN]/api/campaigns/create.php", content);
            var responseString = await response.Content.ReadAsStringAsync();
            return responseString;
        }
    }

FormUrlEncodedContent 的大小受到限制。

服务器接受JSON?如果是这样,你冷试试这个:

var content = new StringContent(serializedValues, Encoding.UTF8, "application/json");

而不是

 var content = new FormUrlEncodedContent(values);

如果不行,您可以尝试使用 this 解决方法:

 var items = formData.Select(i => WebUtility.UrlEncode(i.Key) + "=" + WebUtility.UrlEncode(i.Value));
var content = new StringContent(String.Join("&", items), null, "application/x-www-form-urlencoded");
var response = await client.PostAsync(url, content);