【问题标题】:Why Is The Imgur API slow compared to the website为什么 Imgur API 比网站慢
【发布时间】:2012-10-18 06:43:30
【问题描述】:
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;
    }
}

上面是我编写的一个类,用于使用Anonymous API 将图像上传到 imgur。我过去曾使用过 API,并且一直发现它比网站上传器慢得多,并且比其他使用 Web 请求有效地将数据直接发送到网站而不是使用 API 的 .NET 应用程序慢。

我在上面发布了完整的课程,因为这可能是我正在做(或不做)的事情导致了问题。如果有人能为我找出问题所在,我将不胜感激。

我今天早些时候做了一些公平的测试,例如,一个结果如下:

  • 800kb 图片来自 imgur 网站 = 35 秒
  • 800kb 图片来自 my class = 1 分 20 秒

【问题讨论】:

    标签: c# webclient imgur


    【解决方案1】:

    您上传的文件要大约 35%,因为您将其上传为 STRING。

    通过字节上传,应该一样快。

    【讨论】:

    • 是否仍然可以使用新的 API 版本 3 将图像作为字节上传到 Imgur?当我尝试将图像数据作为字节(使用File.ReadAllBytes())发送到新 API 时,我收到了无效的图像格式错误。不过,通过 base64 转换上传效果很好。
    猜你喜欢
    • 2011-01-08
    • 2017-04-16
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多