上传到imgur.com [英] Uploading to imgur.com

查看:219
本文介绍了上传到imgur.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imgur 是一个图片上传网站谁提供的 API上传



我的代码看起来完全一样,他们提供作为PHP代码例。然而,在他们的PHP代码,他们是 http_build_query($ pvars);



好像他们是URL编码的查询之前张贴。编辑:请注意,我已经改变了充分.NET 3.5而不是客户端配置文件。这给了我进入的System.Web 所以我用 httputliity.urlencode()。这使得API返回一个失败与无图像派。如果我不那么编码API返回一个好的链接到图片,但没有图片上传(像一个空白文件)。



如何解决我的代码来对他们的API正常工作?



 形象画像= Image.FromFile(C:\\Users\\Affan\\Pictures\\1509310.jpg); 
的MemoryStream毫秒​​=新的MemoryStream();
//图像转换为byte []
image.Save(MS,System.Drawing.Imaging.ImageFormat.Jpeg);
字节[] = imageBytes ms.ToArray();

的WebRequest WB = WebRequest.Create(新的URI(http://imgur.com/api/upload.xml));
wb.ContentType =应用/的X WWW的形式,进行了urlencoded
wb.Method =POST;
wb.Timeout = 10000;
Console.WriteLine(imageBytes.Length);
字符串参数=键= 433a1bf4743dd8d7845629b95b5ca1b4&安培;图像=+ Convert.ToBase64String(imageBytes);


Console.WriteLine(参​​数:+ parameters.Length);
System.Text.UTF8Encoding编码=新System.Text.UTF8Encoding();
字节[]字节= encoding.GetBytes(参数);
//字节[]字节= Convert.FromBase64String(参数);

的System.IO.Stream OS = NULL;
试{//发送邮报
wb.ContentLength = bytes.Length; //数字节发送
OS = wb.GetRequestStream();
os.Write(字节,0,bytes.Length); //发送它
}赶上(引发WebException前){
MessageBox.Show(ex.Message,HttpPost:请求错误);
Console.WriteLine(ex.Message);
} {最后
如果(OS!= NULL){
// os.Close();
}
}

试{//得到响应
WebResponse类WebResponse类= wb.GetResponse();

StreamReader的SR =新的StreamReader(webResponse.GetResponseStream());
//MessageBox.Show(sr.ReadToEnd()修剪());
Console.WriteLine(sr.ReadToEnd()修剪());
}赶上(引发WebException前){
MessageBox.Show(ex.Message,HttpPost:响应错误);
}


解决方案

我刚刚上传了该图片





使用此代码:

 使用(VAR W =新的WebClient())
{
VAR值=新的NameValueCollection
{
{键,433a1bf4743dd8d7845629b95b5ca1b4},
{形象,Convert.ToBase64String(File.ReadAllBytes(@hello.png ))}
};

字节[] =响应w.UploadValues​​(http://imgur.com/api/upload.xml,值);

Console.WriteLine(XDocument.Load(新的MemoryStream(响应)));
}

您可能会想现在就改变你的API密钥: - )



输出是:

 <可吸入悬浮粒子STAT =OK> 
< image_hash> IWg2O< / image_hash>
< delete_hash> fQAXiR2Fdq< / delete_hash>
< original_image> HTTP://i.imgur.com/IWg2O.png< / original_image>
< large_thumbnail> HTTP://i.imgur.com/IWg2Ol.jpg< / large_thumbnail>
< small_thumbnail> HTTP://i.imgur.com/IWg2Os.jpg< / small_thumbnail>
< imgur_page> HTTP://imgur.com/IWg2O< / imgur_page>
< delete_page> HTTP://imgur.com/delete/fQAXiR2Fdq< / delete_page>
< / RSP>


Imgur is a image uploading website who offers an API to upload

My code looks exactly like the PHP code they provide as an example. however, in their php code they are http_build_query($pvars);

It seems like they are URLEncoding their query before posting it. edit: Note that I have changed to full .NET 3.5 rather then the client profile. This gave me access to system.web so I used httputliity.urlencode(). This made the api return a "fail" with a "no image was sent". If I don't encode then the API returns an "okay" with a link to the picture, however no picture is uploaded (like a blank file).

How can I fix my code to work properly against their API?

 Image image = Image.FromFile("C:\\Users\\Affan\\Pictures\\1509310.jpg");
        MemoryStream ms = new MemoryStream();
        // Convert Image to byte[]
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] imageBytes = ms.ToArray();

        WebRequest wb = WebRequest.Create(new Uri("http://imgur.com/api/upload.xml"));
        wb.ContentType = "application/x-www-form-urlencoded";            
        wb.Method = "POST";
        wb.Timeout = 10000;
        Console.WriteLine(imageBytes.Length);
        string parameters = "key=433a1bf4743dd8d7845629b95b5ca1b4&image=" + Convert.ToBase64String(imageBytes);


        Console.WriteLine("parameters: " + parameters.Length);
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byte[] bytes = encoding.GetBytes(parameters);
        // byte[] bytes = Convert.FromBase64String(parameters);

        System.IO.Stream os = null;
        try { // send the Post
            wb.ContentLength = bytes.Length;   //Count bytes to send
            os = wb.GetRequestStream();               
            os.Write(bytes, 0, bytes.Length);         //Send it
        } catch (WebException ex) {
            MessageBox.Show(ex.Message, "HttpPost: Request error");
            Console.WriteLine(ex.Message);
        } finally {
            if (os != null) {
               // os.Close();
            }
        }

        try { // get the response
            WebResponse webResponse = wb.GetResponse();

            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            //MessageBox.Show(sr.ReadToEnd().Trim());
            Console.WriteLine(sr.ReadToEnd().Trim());
        } catch (WebException ex) {
            MessageBox.Show(ex.Message, "HttpPost: Response error");
        }       

解决方案

I've just uploaded this image

using this code:

using (var w = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key", "433a1bf4743dd8d7845629b95b5ca1b4" },
        { "image", Convert.ToBase64String(File.ReadAllBytes(@"hello.png")) }
    };

    byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);

    Console.WriteLine(XDocument.Load(new MemoryStream(response)));
}

You might want to change your API key now :-)

The output was:

<rsp stat="ok">
  <image_hash>IWg2O</image_hash>
  <delete_hash>fQAXiR2Fdq</delete_hash>
  <original_image>http://i.imgur.com/IWg2O.png</original_image>
  <large_thumbnail>http://i.imgur.com/IWg2Ol.jpg</large_thumbnail>
  <small_thumbnail>http://i.imgur.com/IWg2Os.jpg</small_thumbnail>
  <imgur_page>http://imgur.com/IWg2O</imgur_page>
  <delete_page>http://imgur.com/delete/fQAXiR2Fdq</delete_page>
</rsp>

这篇关于上传到imgur.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆