繁体   English   中英

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

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

以下代码正在构建对 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);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM