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

Viewed 3581

The below code is used to send emails through the 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)

It was working great until I noticed a few days ago it started throwing the following exception. Unfortunately it isn't consistent because it will send 30/40 emails but throw exceptions for the other 10, and the number of exceptions each day has been different and for different people. I know the data im sending in the multipartform is large but that shouldn't be part of the uri and unless someone has a 1500 character email the uri should never be too long. Does anyone have any idea of what may be going on? I appreciate any help.

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

3 Answers

This is a known issue in pretty much all flavors of .NET. Even though the exception message says "Invalid URI", you'll notice from the stack trace that it's being thrown from FormUrlEncodedContent. So the body of the request is the problem.

One way to get around this is to use Flurl (disclaimer: I'm the author) to make the request. I've explicitly fixed this issue in Flurl's implementation. And as a bonus it will clean up your URL-building and content-building code significantly:

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

I had similar issue while using the "MultipartFormDataContent"and I wouldn't recommand to use it.

Now, I use JSON for that.

You might find more info right here : https://stackoverflow.com/a/38440832/6488079

Hope it helps !

Sorry, I found a fix for me the day after posting this and moved on forgetting about the post. I changed the api calls to synchronous and haven't seen an error since. I think because the api i'm having to use limits you to 5 concurrent calls maybe having them asynchronous caused an access amount of calls and therefor errors.

Related