/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="method"></param> /// <param name="id"></param> /// <param name="args"></param> /// <returns></returns> public async Task <JsonRpcResponse <T> > ExecuteAsync <T>(string method, int id, params object[] args) { var ps = new JsonRpcRequest() { Method = method, Params = args, Id = id }; this.OnExecuting(new EventArgs <JsonRpcRequest>(ps)); var jsonOut = JsonConvert.SerializeObject(ps.Values); var url = this.ServiceUrl; try { string jsonRet = null; JsonRpcResponse <T> ret = null; using (var client = HttpClientHelper.GetHttpClientWithBasicAuth(url, _Connection.Username, _Connection.Password)) { var httpContent = new StringContent(jsonOut, Encoding.UTF8, "application/json-rpc"); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(url), Content = httpContent, }; var result = await client.SendAsync(request); jsonRet = await result.Content.ReadAsStringAsync(); } try { ret = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(jsonRet); } catch (Exception jsonEx) { var errorResponse = JsonConvert.DeserializeObject <JsonRpcErrorResponse>(jsonRet); throw new JsonRpcException(errorResponse.Error); } ret.Raw = jsonRet; if (ret.Error == null && !ret.Id.HasValue && ret.Result == null) { ret = null; } return(ret); } catch (Exception ex) when(!(ex is JsonRpcException)) { var nEXt = ex; string errormsg = null; while (nEXt != null) { if (nEXt is WebException) { var webEx = (WebException)nEXt; if (webEx.Response != null) { HttpWebResponse resp = (HttpWebResponse)webEx.Response; if (resp.StatusCode != HttpStatusCode.OK) { if (resp.ContentType != "application/json") { throw ex; } } using (var stream = webEx.Response.GetResponseStream()) errormsg = new StreamReader(stream).ReadToEnd(); } if (errormsg == null) { throw ex; } if (errormsg == "Forbidden") { throw ex; } break; } nEXt = nEXt.InnerException; } var errorResponse = JsonConvert.DeserializeObject <JsonRpcErrorResponse>(errormsg); throw new JsonRpcException(errorResponse.Error); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="method"></param> /// <param name="id"></param> /// <param name="args"></param> /// <returns></returns> public async Task <JsonRpcResponse <T> > ExecuteAsync <T>(string method, int id, params object[] args) { var ps = new JsonRpcRequest() { Method = method, Params = args, Id = id }; this.OnExecuting(new EventArgs <JsonRpcRequest>(ps)); var jsonOut = JsonConvert.SerializeObject(ps.Values); var url = this.ServiceUrl; try { string jsonRet = null; JsonRpcResponse <T> ret = null; var request = WebRequest.CreateHttp(url); request.ContentType = "application/json-rpc"; request.Credentials = this.GetCredentials(); request.Method = "POST"; //request.UserAgent = "LucidOcean.MultiChain {version 0}"; var bs = Encoding.UTF8.GetBytes(jsonOut); using (var stream = await request.GetRequestStreamAsync()) { stream.Write(bs, 0, bs.Length); } var response = await request.GetResponseAsync(); using (var stream = ((HttpWebResponse)response).GetResponseStream()) { jsonRet = await new StreamReader(stream).ReadToEndAsync(); } try { ret = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(jsonRet); } catch (Exception jsonEx) { JsonRpcErrorResponse errorobj = JsonConvert.DeserializeObject <JsonRpcErrorResponse>(jsonRet); throw new JsonRpcException($"({errorobj.Error.Code}) {errorobj.Error.Message}"); } ret.Raw = jsonRet; if (ret.Error == null && !ret.Id.HasValue && ret.Result == null) { ret = null; } return(ret); } catch (Exception ex) when(!(ex is JsonRpcException)) { var nEXt = ex; string errormsg = null; while (nEXt != null) { if (nEXt is WebException) { var webEx = (WebException)nEXt; if (webEx.Response != null) { HttpWebResponse resp = (HttpWebResponse)webEx.Response; if (resp.StatusCode != HttpStatusCode.OK) { if (resp.ContentType != "application/json") { throw ex; } } using (var stream = webEx.Response.GetResponseStream()) errormsg = new StreamReader(stream).ReadToEnd(); } if (errormsg == null) { throw ex; } if (errormsg == "Forbidden") { throw ex; } break; } nEXt = nEXt.InnerException; } JsonRpcErrorResponse errorobj = JsonConvert.DeserializeObject <JsonRpcErrorResponse>(errormsg); throw new JsonRpcException($"({errorobj.Error.Code}) {errorobj.Error.Message}"); } }