Загрузка фотографий на стену пользователя "вконтакте"


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:10 p.m.

FORMAT: Text only

SIZE: 7.4 kB

HITS: 1542

  1. Код загрузки фотографии на сервер:
  2. //ЗАГРУЗКА ФОТОГРАФИИ
  3. //получение адреса сервера для загрузки фотографии
  4. System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  5. System.Net.WebRequest post_request2 = System.Net.WebRequest.Create("https://api.vkontakte.ru/method/photos.getWallUploadServer?gid=100669061&access_token=" + textBox3.Text.Trim());
  6. System.Net.WebResponse post_request_response2 = post_request2.GetResponse();
  7. System.IO.Stream post_request_stream2 = post_request_response2.GetResponseStream();
  8. System.IO.StreamReader post_request_stream_reader2 = new System.IO.StreamReader(post_request_stream2);
  9. string post_request_answer2 = post_request_stream_reader2.ReadToEnd();
  10. string[] words_empty = post_request_answer2.Split('"');
  11. string s = words_empty[5];
  12. s = s.Replace("\\/","/");
  13. s = s.Replace("\\/","/");
  14. s = s.Replace("\\/","/");
  15. textBox6.Text = s;
  16. //отправка файла на полученый сервер
  17. NameValueCollection nvc = new NameValueCollection();
  18. //nvc.Add("user", "user");
  19. //nvc.Add("passwd", "passwd");
  20. string s9;
  21. s9 = HttpUploadFile(s, @"C:\file_1.jpg", "photo", "image/jpeg", nvc);
  22. textBox6.Text = s9;
  23. char[] delimiterChars = { ':', ',' };
  24. string[] parameters = s9.Split(delimiterChars);
  25. string server = parameters[1];
  26. char[] delimiterChars2={':'};
  27. string[] parameters2 = s9.Split(delimiterChars2);
  28. string hash = parameters2[7];
  29. char[] delimiterChars3 = { '"' };
  30. string[] parameters3 = hash.Split(delimiterChars3);
  31. hash = parameters3[1];
  32. string photo = s9.Substring(s9.IndexOf("photo")+8, s9.IndexOf("hash") - s9.IndexOf("photo")-11);
  33. photo = photo.Replace("\\\"", "\"");
  34. //сохранение фотографии на сервере
  35. string url3 = "https://api.vk.com/method/photos.saveWallPhoto?server=" + server + "&photo=" + HttpUtility.UrlEncode(photo) + "&hash=" + hash + "&access_token=" + textBox3.Text.Trim();
  36. textBox6.Text = textBox6.Text + " ---------- " + url3;
  37. System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  38. System.Net.WebRequest post_request3 = System.Net.WebRequest.Create(url3);
  39. System.Net.WebResponse post_request_response3 = post_request3.GetResponse();
  40. System.IO.Stream post_request_stream3 = post_request_response3.GetResponseStream();
  41. System.IO.StreamReader post_request_stream_reader3 = new System.IO.StreamReader(post_request_stream3);
  42. string post_request_answer3 = post_request_stream_reader3.ReadToEnd();
  43. textBox6.Text = textBox6.Text + " ---------- " + post_request_answer3;
  44. Код функции HttpUploadFile:
  45. public static string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
  46. {
  47. Console.WriteLine(string.Format("Uploading {0} to {1}", file, url));
  48. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  49. byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  50. HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  51. //wr.ContentType = "multipart/form-data; boundary=" + boundary;
  52. wr.ContentType = "multipart/form-data; boundary=" + boundary;
  53. wr.Method = "POST";
  54. wr.KeepAlive = true;
  55. wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  56. Stream rs = wr.GetRequestStream();
  57. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  58. foreach (string key in nvc.Keys)
  59. {
  60. rs.Write(boundarybytes, 0, boundarybytes.Length);
  61. string formitem = string.Format(formdataTemplate, key, nvc[key]);
  62. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  63. rs.Write(formitembytes, 0, formitembytes.Length);
  64. }
  65. rs.Write(boundarybytes, 0, boundarybytes.Length);
  66. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  67. string header = string.Format(headerTemplate, paramName, file, contentType);
  68. byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  69. rs.Write(headerbytes, 0, headerbytes.Length);
  70. FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
  71. byte[] buffer = new byte[4096];
  72. int bytesRead = 0;
  73. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  74. {
  75. rs.Write(buffer, 0, bytesRead);
  76. }
  77. fileStream.Close();
  78. byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  79. rs.Write(trailer, 0, trailer.Length);
  80. rs.Close();
  81. WebResponse wresp = null;
  82. try
  83. {
  84. wresp = wr.GetResponse();
  85. Stream stream2 = wresp.GetResponseStream();
  86. StreamReader reader2 = new StreamReader(stream2);
  87. //Form Form1 = new Form1();
  88. //Form
  89. //textBox6.Text = reader2.ReadToEnd();
  90. //MessageBox.Show(s3);
  91. //Console.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
  92. string s3 = reader2.ReadToEnd();
  93. return s3;
  94. }
  95. catch (Exception ex)
  96. {
  97. Console.WriteLine("Error uploading file", ex);
  98. if (wresp != null)
  99. {
  100. wresp.Close();
  101. wresp = null;
  102. }
  103. wresp = wr.GetResponse();
  104. Stream stream2 = wresp.GetResponseStream();
  105. StreamReader reader2 = new StreamReader(stream2);
  106. string s3 = reader2.ReadToEnd();
  107. return s3;
  108. }
  109. finally
  110. {
  111. wr = null;
  112. }
  113. }

comments powered by Disqus