无效的 URI:尝试将值字典发布到 API 时,Uri 字符串太长



下面的代码正在构造对 API 的 post 请求,但在某些情况下,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);

如果没有,您可以尝试使用此解决方法:

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

最新更新