views:

279

answers:

1

I dontknow if any of you know, but imgur.com is a image uploading website. They do offer an API to upload to their server.

Information at: http://code.google.com/p/imgur-api/wiki/ImageUploading

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

I googled this function, and it seems like they are URLEncoding their query before posting it. edit: Note that I have changed to full .net3.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).

Here is my code:

 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");
        }       

So, can anyone look at their python/php example and asee what I am doing wrong in myne? thanks a lot.

+3  A: 

I've just uploaded this image

Hello World

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&lt;/original_image&gt;
  <large_thumbnail>http://i.imgur.com/IWg2Ol.jpg&lt;/large_thumbnail&gt;
  <small_thumbnail>http://i.imgur.com/IWg2Os.jpg&lt;/small_thumbnail&gt;
  <imgur_page>http://imgur.com/IWg2O&lt;/imgur_page&gt;
  <delete_page>http://imgur.com/delete/fQAXiR2Fdq&lt;/delete_page&gt;
</rsp>
dtb
WOW, i spent almost 5 hours on that.. thankyou for that. Can you figure out why my way wasnt working though?
masfenix
Nice answer! I always love a 'less-is-more' solution. Granted, you went for the bare-bones with no error-checking, but even adding that back in... Yep, nice one.
JMD