Search code examples
c#webclientimgur

Why Is The Imgur API slow compared to the website


public sealed class ImgurUpload
{
    public event EventHandler<UploadCompleteEventArgs> UploadComplete;

    public void PostToImgur(string location, string key, string name = "", string caption = "")
    {
        try
        {
            using (var webClient = new WebClient())
            {
                NameValueCollection values = new NameValueCollection
                {
                    { "image", ConvertToBase64(location) },
                    { "key", key },
                    { "name", name },
                    { "caption", caption}
                };
                webClient.UploadValuesAsync(new Uri("http://api.imgur.com/2/upload.xml"), "POST", values);
                webClient.UploadValuesCompleted += ((sender, eventArgs) =>
                {
                    byte[] response = eventArgs.Result;
                    XDocument result = XDocument.Load(XmlReader.Create(new MemoryStream(response)));
                    if (UploadComplete != null)
                        UploadComplete(this, new UploadCompleteEventArgs(result));
                });
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);  
        }
    }

    private string ConvertToBase64(string imageLocation)
    {
        byte[] imageData = null;
        using (FileStream fileStream = File.OpenRead(imageLocation))
        {
            imageData = new byte[fileStream.Length];
            fileStream.Read(imageData, 0, imageData.Length);
        }
        return Convert.ToBase64String(imageData);
    }
}

public class UploadCompleteEventArgs : EventArgs
{
    public string Original { get; private set; }
    public string ImgurPage { get; private set; }
    public string DeletePage { get; private set; }

    public UploadCompleteEventArgs(XDocument xmlDoc)
    {
        var objLinks = from links in xmlDoc.Descendants("links")
                       select new
                       {
                           original = links.Element("original").Value,
                           imgurPage = links.Element("imgur_page").Value,
                           deletePage = links.Element("delete_page").Value
                       };

        Original = objLinks.FirstOrDefault().original;
        ImgurPage = objLinks.FirstOrDefault().imgurPage;
        DeletePage = objLinks.FirstOrDefault().deletePage;
    }
}

Above is a class I wrote to upload an image to imgur using the Anonymous API. I have used the API in the past and have always found it to be considerably slower than the website uploader and slower than other .NET applications that use web requests to effectively send data to the website directly rather than using the API.

I posted the full class above as it may be something I'm doing (or not doing) that's causing the issue. I'd really appreciate it if anybody can identify the issue for me.

I did some fair testing earlier today and one result for example, is as followed:

  • 800kb image via the imgur website = 35 seconds
  • 800kb image via using my class = 1minute 20 seconds

Solution

  • The one you are uploading is ~35% bigger because you're uploading it as a STRING.

    Upload via bytes and it should be just as fast.