Post.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. namespace HyperCube
  9. {
  10. public class Post
  11. {
  12. public static async Task<string> PostRequestAsync(string json)
  13. {
  14. Console.WriteLine($"json req {json} len {json.Length}");
  15. var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8545");
  16. httpWebRequest.ContentType = "application/json";
  17. httpWebRequest.Method = "POST";
  18. httpWebRequest.Accept = "application/json";
  19. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  20. {
  21. streamWriter.Write(json);
  22. }
  23. HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
  24. using (Stream stream = response.GetResponseStream())
  25. {
  26. using (StreamReader reader = new StreamReader(stream))
  27. {
  28. //Console.WriteLine(reader.ReadToEnd());
  29. var result = reader.ReadLine();
  30. Console.WriteLine($"json result {result} len {result.Length}");
  31. response.Close();
  32. return result;
  33. }
  34. }
  35. //using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  36. //{
  37. // streamWriter.Write(json);
  38. //}
  39. //var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  40. //using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  41. //{
  42. // var result = streamReader.ReadToEnd();
  43. // Console.WriteLine($"json result {result} len {result.Length}");
  44. // answer = result;
  45. //}
  46. }
  47. }
  48. }