1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.IO;
- using System.Net;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube
- {
- public class Post
- {
- public static async Task<string> PostRequestAsync(string json)
- {
- Console.WriteLine($"json req {json} len {json.Length}");
- var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8545");
- httpWebRequest.ContentType = "application/json";
- httpWebRequest.Method = "POST";
- httpWebRequest.Accept = "application/json";
-
- using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
- {
- streamWriter.Write(json);
- }
- HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
- using (Stream stream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(stream))
- {
- //Console.WriteLine(reader.ReadToEnd());
- var result = reader.ReadLine();
- Console.WriteLine($"json result {result} len {result.Length}");
- response.Close();
- return result;
- }
- }
-
- //using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
- //{
- // streamWriter.Write(json);
- //}
- //var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
- //using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
- //{
- // var result = streamReader.ReadToEnd();
- // Console.WriteLine($"json result {result} len {result.Length}");
- // answer = result;
- //}
- }
- }
- }
|