Uri is longer than the maximum 32766 characters

LucculLuccul FRMember ✭✭
edited May 2016 in Xamarin.Forms


I want to create my own Imgur uploader with Xamarin.Forms MVVM.

But i have an issue when i want to convert my image in base64 to upload in the url.

I have this error:

Uri is longer than the maximum 32766 characters.

And yes, look my byte

I made this code to try to fix it but not working

private string ImageToBase64(Stream stream)
            var memoryStream = new MemoryStream();
            var result = memoryStream.ToArray(); 

            var base64img = Convert.ToBase64String(result);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < base64img.Length; i += 32766)
                sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(32766, base64img.Length - i))));

            return sb.ToString();

    public async Task<ImgurImage> UploadImageAnonymous(Stream imageStream)
                using (HttpClient client = new HttpClient())

                    string base64Image = ImageToBase64(imageStream);

                    var formContent = new FormUrlEncodedContent(new[] { 
                        new KeyValuePair<string, string>("image", base64Image)
                    HttpResponseMessage response = await client.PostAsync(new Uri(BaseUrl + "upload"), formContent);
                    await CheckHttpStatusCode(response);
                    string content = await response.Content.ReadAsStringAsync();

                    ImgurRootObject<ImgurImage> imgRoot = JsonConvert.DeserializeObject<ImgurRootObject<ImgurImage>>(content);

                    return imgRoot.Data;

Can you help me ?

Best Answer


  • AdamPAdamP AUUniversity ✭✭✭✭✭
    edited May 2016

    @Naografix - It would be better to put the image in a POST body, rather than the URL, which you are already doing anyway.

    What is the url?

Sign In or Register to comment.