using System; using System.Net.Http; using System.Threading.Tasks; using System.Collections.Generic; namespace sharp.jsonrpc { public class RPCConnector { private readonly HttpClient http; public RPCConnector(string hostname,int port) { this.http = new HttpClient(); this.Hostname = hostname; this.Port = port; } public String Hostname { get; private set; } public int Port { get; private set; } public string URL { get { return String.Format("http://{0}:{1}/json_rpc",Hostname,Port); } } public RPCCall Call(){ return new RPCCall(this); } public Response Call(string method) { return Call(method, null); } public Response Call(string method,Dictionary args){ try { string request = sharp.jsonrpc.Request.Create(method,args); //Console.WriteLine("json-call: {0}",request); Task response = http.PostAsync(URL, new StringContent(request)); response.Wait(); Task result = response.Result.Content.ReadAsStringAsync(); result.Wait(); return new Response(result.Result); } catch (HttpRequestException e) { Response resp = new Response(String.Format("{ \"id\": \"0\", \"sharp.jsonrpc\": \"2.0\", \"result\": { status: \"Error\", message: \"HttpException: {0}\"}", e.ToString())); return resp; } } } }