上传到imgur.com

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

我的代码看起来与它们提供的PHP代码完全相同。 但是,在他们的PHP代码中,他们是http_build_query($pvars);

看起来他们在发布之前会对他们的查询进行URLE编码。 编辑:请注意,我已经更改为完整的.NET 3.5而不是客户端配置文件。 这让我可以访问system.web所以我使用了httputliity.urlencode() 。 这使得api返回“失败”并显示“没有图像被发送”。 如果我没有编码,那么API返回一个“okay”,其中包含指向图片的链接,但是没有上传图片(如空白文件)。

如何修复我的代码以适应其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"); } 

我刚刚上传了这张图片

你好,世界

使用此代码:

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

您现在可能想要更改API密钥:-)

输出是:

  IWg2O fQAXiR2Fdq http://i.imgur.com/IWg2O.png http://i.imgur.com/IWg2Ol.jpg http://i.imgur.com/IWg2Os.jpg http://imgur.com/IWg2O http://imgur.com/delete/fQAXiR2Fdq  

以下是使用匿名上传的v3 API的dtb答案的更新版本(您需要在http://api.imgur.com/注册您的应用以获取您的客户端ID):

 using (var w = new WebClient()) { string clientID = "<>"; w.Headers.Add("Authorization", "Client-ID " + clientID); var values = new NameValueCollection { { "image", Convert.ToBase64String(File.ReadAllBytes(@"hello.png")) } }; byte[] response = w.UploadValues("https://api.imgur.com/3/upload.xml", values); Console.WriteLine(XDocument.Load(new MemoryStream(response))); } 

响应现在就像这样(见http://api.imgur.com/models/image ):

  SbBGk   1341533193 image/jpeg false 2559 1439 521916 1 521916 eYZd3NNJHsbreD1 
http://i.imgur.com/SbBGk.jpg

我猜不推荐使用dtb解决方案

  using (var w = new WebClient()) { var values = new NameValueCollection { {"image", Convert.ToBase64String(imageData)}, {"type", "base64"} }; w.Headers.Add("Authorization", "Client-ID xxxxxxxxx"); var response = w.UploadValues("https://api.imgur.com/3/image", values); } 

另一种方法:

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image"); request.Headers.Add("Authorization", "Client-ID xxxxxxx"); request.Method = "POST"; ASCIIEncoding enc = new ASCIIEncoding(); string postData = Convert.ToBase64String(imageData); byte[] bytes = enc.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bytes.Length; Stream writer = request.GetRequestStream(); writer.Write(bytes, 0, bytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse();