Post.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Console = HyperCube.Utils.AdvConsole;
  8. using HyperCube.Models;
  9. namespace HyperCube
  10. {
  11. public class Post
  12. {
  13. public static async Task<string> PostRequestAsync(Blockchain bc, string json)
  14. {
  15. if (bc != null)
  16. {
  17. Console.WriteLine($"json req {json} len {json.Length}");
  18. string url = $"http://{bc.url}:{bc.port}";
  19. var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  20. httpWebRequest.ContentType = "application/json";
  21. httpWebRequest.Method = "POST";
  22. httpWebRequest.Accept = "application/json";
  23. Console.WriteLine($"PostRequestAsync {url}");
  24. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  25. {
  26. streamWriter.Write(json);
  27. }
  28. HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
  29. using (Stream stream = response.GetResponseStream())
  30. {
  31. using (StreamReader reader = new StreamReader(stream))
  32. {
  33. //Console.WriteLine(reader.ReadToEnd());
  34. var result = reader.ReadLine();
  35. Console.WriteLine($"json result {result} len {result.Length}");
  36. response.Close();
  37. return result;
  38. }
  39. }
  40. }
  41. return "null";
  42. }
  43. }
  44. }