【问题标题】:uploading image to imgur using c#使用 c# 将图像上传到 imgur
【发布时间】:2011-05-20 09:24:21
【问题描述】:

使用以下代码将图像上传到 imgur.com 会返回 http 400 错误代码。我的开发人员密钥是正确的,我尝试了不同的图像格式,大小最大为 70 kb。 我还尝试了http://api.imgur.com/examples 给出的 c# 代码示例,但它也给出了 http 400。 可能是什么问题?

public XDocument Upload(string imageAsBase64String)
{
    XDocument result = null;
    using (var webClient = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "key", key },
            { "image", imageAsBase64String },
            { "type", "base64" },
        };
        byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
        result = XDocument.Load(new MemoryStream(response));
    }
    return result;
}

编辑:这是一个 ASP.NET MVC 应用程序,调用者控制器操作是:

[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        var imgService = new ImgUrImageService();
        byte[] fileBytes = new byte[uploadFile.InputStream.Length];
        Int64 byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.InputStream.Length);
        uploadFile.InputStream.Close();
        string fileContent = Convert.ToBase64String(fileBytes, 0, fileBytes.Length);
        var response = imgService.Upload(fileContent);
    }
    return View();
}

【问题讨论】:

  • 你有没有得到这个工作?
  • 不,还是一样。这是我在家里做的一个副项目。我想我会看看其他图像服务。

标签: c# uploading imgur


【解决方案1】:

好的,我找到了原因。我的 web.config 文件中的代理设置(用于 Fiddler)导致了这个问题。删除它解决了问题和我的另一个问题(与recaptcha 相关)。代码就像一个魅力。

【讨论】:

    【解决方案2】:

    如果您将代码修改为:

    public XDocument Upload(string imageAsBase64String)
    {
        XDocument result = null;
        using (var webClient = new WebClient())
        {
            var values = new NameValueCollection
                {
                    { "key", key },
                    { "image", imageAsBase64String }
                };
            byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
            result = XDocument.Load(System.Xml.XmlReader.Create(new MemoryStream(response)));
        }
        return result;
    }
    

    使用 ANONYMOUS API 密钥一切正常。要使用经过身份验证的 API,您必须使用您的 Consumer Key 和 Consumer Secret 创建一个 OAuth 令牌。

    Imgur 提供了有关所需特定端点的更多信息以及一些指向其他帮助的链接:http://api.imgur.com/auth

    你的转换代码看起来基本没问题,我稍微改动了一下:

    [HttpPost]
    public ActionResult UploadImage(HttpPostedFile uploadFile)
    {
        if (uploadFile.ContentLength > 0)
        {
            var imgService = new ImgUrImageService();
            byte[] fileBytes = new byte[uploadFile.ContentLength];
            uploadFile.InputStream.Read(fileBytes, 0, fileBytes.Length);
            uploadFile.InputStream.Close();
            string fileContent = Convert.ToBase64String(fileBytes);
            var response = imgService.Upload(fileContent);
        }
        return View();
    }
    

    在您的原始上传代码中,您添加了一个额外的类型值,您是否仍在添加此值,或者您是否切换了代码以匹配我上面更改的代码?我认为没有理由添加此值,也看不到 imgur 支持的位置。

    【讨论】:

    • 克里斯感谢您的回答。此代码仍使用我的开发人员密钥返回代码 400。上传是匿名 API 方法。所以我的开发者密钥应该足够了。我的开发者密钥不也是匿名密钥吗?我的本地机器可能有问题吗?
    • 我用你的密钥试了我的代码,还是一样。我还编辑了我的问题并添加了转换代码。
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多