Post.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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(string json)
  14. {
  15. var main = Blockchain.GetMain();
  16. if (main != null)
  17. {
  18. Console.WriteLine($"json req {json} len {json.Length}");
  19. string url = $"http://{main.url}:{main.port}";
  20. var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  21. httpWebRequest.ContentType = "application/json";
  22. httpWebRequest.Method = "POST";
  23. httpWebRequest.Accept = "application/json";
  24. Console.WriteLine($"PostRequestAsync {url}");
  25. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  26. {
  27. streamWriter.Write(json);
  28. }
  29. HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
  30. using (Stream stream = response.GetResponseStream())
  31. {
  32. using (StreamReader reader = new StreamReader(stream))
  33. {
  34. //Console.WriteLine(reader.ReadToEnd());
  35. var result = reader.ReadLine();
  36. Console.WriteLine($"json result {result} len {result.Length}");
  37. response.Close();
  38. return result;
  39. }
  40. }
  41. }
  42. return "null";
  43. }
  44. }
  45. }