0

My code:

string json = BuildJson(uploadItem);

using (var client = new HttpClient())
{
    var values = new List<KeyValuePair<string, string>>();
    values.Add(new KeyValuePair<string, string>("parameter", json));

    var content = new FormUrlEncodedContent(values);
    var response = await client.PostAsync(App.Current.LoginUrl, content);

    var responseString = await response.Content.ReadAsStringAsync();
}

My json string includes an base64 encoded image so the FormUrlEncodedContent throws the exception :

" Invalid URI: The Uri string is too long".

Important is that the server expects exact this format with "parameter" as post key and the json as the post value. How can I bypass this limitation of FormUrlEncodedContent?

user1672994
  • 10,509
  • 1
  • 19
  • 32
ManfredP
  • 1,037
  • 1
  • 12
  • 27

1 Answers1

0

I solved this issue with the following method which replaces FormUrlEncodedContent :

// URI Escape JSON string
var content = EscapeDataString(json);

private string EscapeDataString(string str)
{
    int limit = 2000;

    StringBuilder sb = new StringBuilder();
    int loops = str.Length / limit;

    for (int i = 0; i <= loops; i++)
    {
        if (i < loops)
        {
            sb.Append(Uri.EscapeDataString(str.Substring(limit * i, limit)));
        }
        else
        {
            sb.Append(Uri.EscapeDataString(str.Substring(limit * i)));
        }
    }

    return sb.ToString();
}
ManfredP
  • 1,037
  • 1
  • 12
  • 27