Visual Basic Imgur Upload


SUBMITTED BY: Guest

DATE: Nov. 13, 2013, 9:06 p.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 692

  1. 'www.youtube.com/teachComputing
  2. 'Import : System.IO, System.Net, System.Web, System.Text
  3. 'Ensure you add reference : system.web (.NET TAB)
  4. 'Also ensure your target framework is set to .NET 4.0 not Client profile!
  5. #Region "Imgur Upload"
  6. Private Sub PostToImgur()
  7. Dim apiKey As String = "b9b03e223918469b258194599f5ef68b"
  8. Dim imageData() As Byte
  9. Dim fileStream As FileStream = File.OpenRead(path)
  10. imageData = New Byte((fileStream.Length) - 1) {}
  11. fileStream.Read(imageData, 0, imageData.Length)
  12. fileStream.Close()
  13. Dim uploadRequestString As String = HttpUtility.UrlEncode("image", Encoding.UTF8) + "=" + HttpUtility.UrlEncode(System.Convert.ToBase64String(imageData)) + "&" + HttpUtility.UrlEncode("key", Encoding.UTF8) + "=" + HttpUtility.UrlEncode(apiKey, Encoding.UTF8)
  14. Dim webRequest As HttpWebRequest = CType(Net.WebRequest.Create("http://api.imgur.com/2/upload"), HttpWebRequest)
  15. webRequest.Method = "POST"
  16. webRequest.ContentType = "application/x-www-form-urlencoded"
  17. webRequest.ServicePoint.Expect100Continue = False
  18. Dim streamWriter As IO.StreamWriter = New IO.StreamWriter(webRequest.GetRequestStream)
  19. streamWriter.Write(uploadRequestString)
  20. streamWriter.Close()
  21. Dim response As WebResponse = webRequest.GetResponse
  22. Dim responseStream As Stream = response.GetResponseStream
  23. Dim responseReader As StreamReader = New StreamReader(responseStream)
  24. Dim responseString As String = responseReader.ReadToEnd
  25. Dim ImageURL As String = Split(responseString, "<original>")(1).Split("<")(0)
  26. TextBox1.Text = ImageURL
  27. End Sub
  28. #End Region

comments powered by Disqus