.Net HttpClient 无效的 URI:Uri 字符串太长

.Net HttpClient Invalid URI: The Uri string is too long

以下代码用于通过 pardot api 发送电子邮件。

if (ConfigurationManager.AppSettings.Count > 0)
        {
            uri = ConfigurationManager.AppSettings["PardotURI"].ToString() + "email/version/4/do/send/prospect_email/" + email;
            uri += "?user_key=" + ConfigurationManager.AppSettings["PardotUserKey"].ToString();
            uri += "&api_key=" + GetAPIKey() + "&campaign_id=" + GetPardotCampaign("Capis News");
            uri += "&from_email=" + ConfigurationManager.AppSettings["FromEmail"].ToString();
            uri += "&from_name=" + ConfigurationManager.AppSettings["FromName"].ToString();
            uri += "&name=FlyNews - " + DateTime.Now.ToString("MM/dd/yyy h:mm tt");
            uri += "&subject=CAPIS: Client Holdings News " + DateTime.Today.ToString("MM/dd/yyyy");
        }

        try
        {
            MultipartFormDataContent data = new MultipartFormDataContent();

            data.Add(new StringContent(htmlContent), "html_content");
            data.Add(new StringContent(textContent), "text_content");

            await client.PostAsync(uri, data);
            client.Dispose();
        }
        catch(Exception ex)

它工作得很好,直到几天前我注意到它开始抛出以下异常。不幸的是,它并不一致,因为它会发送 30/40 封电子邮件,但会为其他 10 封邮件抛出异常,而且每天的异常数量因人而异。我知道我在 multipartform 中发送的数据很大,但这不应该是 uri 的一部分,除非有人有 1500 个字符的电子邮件,否则 uri 永远不应该太长。有谁知道可能发生的事情吗?感谢您的帮助。

System.UriFormatException: Invalid URI: The Uri string is too long. at System.UriHelper.EscapeString(String input, Int32 start, Int32 end, Char[] dest, Int32& destPos, Boolean isUriString, Char force1, Char force2, Char rsvd) at System.Uri.EscapeDataString(String stringToEscape) at System.Net.Http.FormUrlEncodedContent.Encode(String data) at System.Net.Http.FormUrlEncodedContent.GetContentByteArray(IEnumerable1 nameValueCollection) at System.Net.Http.FormUrlEncodedContent..ctor(IEnumerable1 nameValueCollection) at PardotDataAccessLibrary.PardotDataAccess.d__9.MoveNext()

我在使用 "MultipartFormDataContent" 时遇到了类似的问题,我不建议使用它。

现在,我使用 JSON。

您可能会在这里找到更多信息:

希望对您有所帮助!

这是几乎所有 .NET 版本中的 known issue。即使异常消息显示 "Invalid URI",您也会从堆栈跟踪中注意到它是从 FormUrlEncodedContent 中抛出的。所以请求的正文就是问题所在。

解决这个问题的一种方法是在 Flurl 的实现中使用 Flurl (disclaimer: I'm the author) to make the request. I've explicitly fixed 这个问题。作为奖励,它将显着清理您的 URL 构建和内容构建代码:

await ConfigurationManager.AppSettings["PardotURI"]
    .AppendPathSegments("email/version/4/do/send/prospect_email", email)
    .SetQueryParams(new {
        user_key = ConfigurationManager.AppSettings["PardotUserKey"],
        pi_key = GetAPIKey() + "&campaign_id=" + GetPardotCampaign("Capis News");
        from_email = ConfigurationManager.AppSettings["FromEmail"],
        from_name = ConfigurationManager.AppSettings["FromName"],
        name = "FlyNews - " + DateTime.Now.ToString("MM/dd/yyy h:mm tt"),
        subject = "CAPIS: Client Holdings News " + DateTime.Today.ToString("MM/dd/yyyy")
    })
    .PostUrlEncodedAsync(new {
        html_content = htmlContent,
        text_content = textContent
    });

抱歉,我在 post 之后的第二天找到了一个修复程序,然后继续忘记 post。我将 api 调用更改为同步,此后没有看到错误。我认为是因为我必须使用的 api 将您限制为 5 个并发调用,可能让它们异步导致调用访问量并因此导致错误。