使用c#上传图像到imgur
使用以下代码将图像上传到 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();
}
Uploading image to imgur.com using code below returns http 400 error code. My developer key is correct and I tried different image formats with sizes upto 70 kb.
I also tried code example for c# given at http://api.imgur.com/examples but it also gives http 400.
What might be the problem?
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;
}
EDIT: This is an ASP.NET MVC application and caller controller action is:
[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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了原因。我的 web.config 文件中的代理设置(针对 Fiddler)导致了该问题。删除它解决了这个问题,也解决了我的另一个问题(与验证码相关)。代码就像魅力一样发挥作用。
Ok I found the reason. A proxy setting (for Fiddler) in my web.config file was causing the issue. Removing it solved the problem and my another issue too (related to recaptcha). Code is working like a charm.
如果您将代码修改为:
使用 ANONYMOUS API 密钥一切都会正常工作。要使用经过身份验证的 API,您必须使用您的消费者密钥和消费者秘密创建 OAuth 令牌。
Imgur 在此处提供了有关所需特定端点的更多信息以及一些其他帮助的链接:http://api.imgur.com /auth
你的转换代码看起来大部分都很好,我稍微改变了它:
在你原来的上传代码中你添加了一个额外的类型值,你还在添加这个还是你切换了你的代码以匹配我上面更改的代码?我认为没有理由添加此值,并且我不知道 imgur 在哪里支持它。
If you modify your code to this:
Everything will work fine with an ANONYMOUS API key. To use the authenticated API you will have to create an OAuth token using your Consumer Key and Consumer Secret.
Imgur has some more information about the specific endpoints needed and some links to additional help here: http://api.imgur.com/auth
Your conversion code looks mostly fine, I have changed it slightly:
In your original upload code you add an extra value of type, are you still adding this or did you switch your code to match my changed code above? I see no reason to add this value and I don't see where it's supported with imgur.