예제 #1
0
        private HttpWebRequest MakeHttpRequest(JsonRpcRequest jsonRpcRequest)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_coinService.Parameters.SelectedDaemonUrl);

            SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
            Byte[] byteArray = jsonRpcRequest.GetBytes();
            webRequest.ContentLength = byteArray.Length;

            try
            {
                using (Stream dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw new RpcException("There was a problem sending the request to the wallet", exception);
            }

            return(webRequest);
        }
예제 #2
0
        public T MakeRequest <T>(RestMethods rpcMethod, params object[] parameters)
        {
            var jsonRpcRequest = new JsonRpcRequest(1, rpcMethod.ToString(), parameters);
            var webRequest     = (HttpWebRequest)WebRequest.Create(_coinService.Parameters.SelectedDaemonUrl);

            SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            webRequest.Proxy       = null;
            webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
            var byteArray = jsonRpcRequest.GetBytes();

            webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;

            try
            {
                using (var dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw new RestException("There was a problem sending the request to the wallet", exception);
            }

            try
            {
                string json;

                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }

                var rpcResponse = JsonConvert.DeserializeObject <JsonRestResponse <T> >(json);
                return(rpcResponse.Result);
            }
            catch (WebException webException)
            {
                #region RPC Internal Server Error (with an Error Code)

                var webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new RestException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                var result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    var jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRestResponse <object> >(result);

                                    var internalServerErrorException = new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message, webException)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RestException(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new RestException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                    }
                }

                #endregion

                #region RPC Time-Out

                if (webException.Message == "The operation has timed out")
                {
                    throw new RpcRequestTimeoutException(webException.Message);
                }

                #endregion

                throw new RestException("An unknown web exception occured while trying to read the JSON response", webException);
            }
            catch (JsonException jsonException)
            {
                throw new RpcResponseDeserializationException("There was a problem deserializing the response from the wallet", jsonException);
            }
            catch (ProtocolViolationException protocolViolationException)
            {
                throw new RestException("Unable to connect to the server", protocolViolationException);
            }
            catch (Exception exception)
            {
                var queryParameters = jsonRpcRequest.Parameters.Cast <string>().Aggregate(string.Empty, (current, parameter) => current + (parameter + " "));
                throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method} with parameters: {queryParameters}. \nException: {exception.Message}");
            }
        }
예제 #3
0
        private T MakeRequest <T>(RpcMethods rpcMethod, Int16 timedOutRequestsCount, params object[] parameters)
        {
            JsonRpcRequest jsonRpcRequest = new JsonRpcRequest(1, rpcMethod.ToString(), parameters);
            HttpWebRequest webRequest     = (HttpWebRequest)WebRequest.Create(_coinService.Parameters.SelectedDaemonUrl);

            SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            webRequest.Proxy       = null;
            webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
            Byte[] byteArray = jsonRpcRequest.GetBytes();
            webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;

            try
            {
                using (Stream dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw new RpcException("There was a problem sending the request to the wallet", exception);
            }

            try
            {
                String json;

                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            String result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }

                JsonRpcResponse <T> rpcResponse = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(json);
                return(rpcResponse.Result);
            }
            catch (WebException webException)
            {
                #region RPC Internal Server Error (with an Error Code)

                HttpWebResponse webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (Stream stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (StreamReader reader = new StreamReader(stream))
                            {
                                String result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    JsonRpcResponse <Object> jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRpcResponse <Object> >(result);

                                    RpcInternalServerErrorException internalServerErrorException = new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message, webException)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RpcException(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                    }
                }

                #endregion

                #region RPC Time-Out

                if (webException.Message == "The operation has timed out")
                {
                    if (_coinService.Parameters.RpcResendTimedOutRequests && ++timedOutRequestsCount <= _coinService.Parameters.RpcTimedOutRequestsResendAttempts)
                    {
                        //  Note: effective delay = delayInSeconds + _rpcRequestTimeoutInSeconds
                        if (_coinService.Parameters.RpcDelayResendingTimedOutRequests)
                        {
                            Double delayInSeconds = _coinService.Parameters.RpcUseBase2ExponentialDelaysWhenResendingTimedOutRequests ? Math.Pow(2, timedOutRequestsCount) : timedOutRequestsCount;

                            if (Debugger.IsAttached)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("RPC request timeout: {0}, will resend {1} of {2} total attempts after {3} seconds (request timeout: {4} seconds)", jsonRpcRequest.Method, timedOutRequestsCount, _coinService.Parameters.RpcTimedOutRequestsResendAttempts, delayInSeconds, _coinService.Parameters.RpcRequestTimeoutInSeconds);
                                Console.ResetColor();
                            }

                            Thread.Sleep((Int32)delayInSeconds * GlobalConstants.MillisecondsInASecond);
                        }

                        return(MakeRequest <T>(rpcMethod, timedOutRequestsCount, parameters));
                    }

                    throw new RpcRequestTimeoutException(webException.Message);
                }

                #endregion

                throw new RpcException("An unknown web exception occured while trying to read the JSON response", webException);
            }
            catch (JsonException jsonException)
            {
                throw new RpcResponseDeserializationException("There was a problem deserializing the response from the wallet", jsonException);
            }
            catch (ProtocolViolationException protocolViolationException)
            {
                throw new RpcException("Unable to connect to the server", protocolViolationException);
            }
            catch (Exception exception)
            {
                String queryParameters = jsonRpcRequest.Parameters.Cast <String>().Aggregate(String.Empty, (current, parameter) => current + (parameter + " "));
                throw new Exception(String.Format("A problem was encountered while calling MakeRpcRequest() for: {0} with parameters: {1}. \nException: {2}", jsonRpcRequest.Method, queryParameters, exception.Message));
            }
        }
예제 #4
0
        //Library Method used to make RPC Calls to FLO wallet.

        public string MakeRequest(string method, params object[] parameters)
        {
            string json;
            var    jsonRpcRequest = new JsonRpcRequest(1, method.ToString(), parameters);

            var byteArray = jsonRpcRequest.GetBytes();

            webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;

            try
            {
                using (var dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw new RpcException("There was a problem sending the request to the wallet", exception);
            }

            try
            {
                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }
                return(json);
                //var rpcResponse = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(json);
                //return rpcResponse.Result;
            }
            catch (WebException webException)
            {
                #region RPC Internal Server Error (with an Error Code)

                var webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                var result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    var jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRpcResponse <object> >(result);

                                    var internalServerErrorException = new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message, webException)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RpcException(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                    }
                }

                #endregion

                #region RPC Time-Out

                if (webException.Message == "The operation has timed out")
                {
                    throw new RpcRequestTimeoutException(webException.Message);
                }

                #endregion

                throw new RpcException("An unknown web exception occured while trying to read the JSON response", webException);
            }
            catch (JsonException jsonException)
            {
                throw new RpcResponseDeserializationException("There was a problem deserializing the response from the wallet", jsonException);
            }
            catch (ProtocolViolationException protocolViolationException)
            {
                throw new RpcException("Unable to connect to the server", protocolViolationException);
            }
            catch (Exception exception)
            {
                var queryParameters = jsonRpcRequest.Parameters.Cast <string>().Aggregate(string.Empty, (current, parameter) => current + (parameter + " "));
                throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method} with parameters: {queryParameters}. \nException: {exception.Message}");
            }
        }
예제 #5
0
        public T MakeRequest <T>(RpcMethods rpcMethod, params object[] parameters)
        {
            if (RpcMethods.xrGetTransactions == rpcMethod || RpcMethods.xrGetBlocks == rpcMethod || RpcMethods.xrService == rpcMethod)
            {
                var parameterList = new List <object>();
                foreach (var param in parameters)
                {
                    if (param != null)
                    {
                        if (!param.GetType().IsArray)
                        {
                            parameterList.Add(param);
                        }
                        else
                        {
                            foreach (var p in (IEnumerable)param)
                            {
                                parameterList.Add(p);
                            }
                        }
                    }
                }
                parameters = parameterList.ToArray();
            }


            var jsonRpcRequest = new JsonRpcRequest(1, rpcMethod.ToString(), parameters);
            var webRequest     = (HttpWebRequest)WebRequest.Create(_coinService.Parameters.SelectedDaemonUrl);

            SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            webRequest.Proxy       = null;
            webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
            var byteArray = jsonRpcRequest.GetBytes();

            webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;

            try
            {
                using (var dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                throw new RpcException("There was a problem sending the request to the wallet", exception);
            }

            try
            {
                string json;
                Console.WriteLine("Executing RPC Call: " + rpcMethod.ToString());
                var timer = new Stopwatch();
                timer.Start();
                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }
                timer.Stop();
                Console.WriteLine("Rpc Call Time Elapsed: {0} ms", timer.ElapsedMilliseconds);

                var rpcResponse = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(json);

                var errorProperty = rpcResponse.Result.GetType().GetProperty("Error");

                if (errorProperty != null)
                {
                    var errorValue = (JsonRpcError)errorProperty.GetValue(rpcResponse.Result);
                    if (errorValue != null)
                    {
                        var internalServerErrorException = new RpcInternalServerErrorException(errorValue.Message)
                        {
                            RpcErrorCode = errorValue.Code
                        };
                        internalServerErrorException.Data["rpcResponse"] = rpcResponse.Result;
                        throw internalServerErrorException;
                    }
                }

                return(rpcResponse.Result);
            }
            catch (WebException webException)
            {
                #region RPC Internal Server Error (with an Error Code)

                var webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                var result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    var jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRpcResponse <object> >(result);

                                    var internalServerErrorException = new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message, webException)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RpcException(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                    }
                }

                #endregion

                #region RPC Time-Out

                if (webException.Message == "The operation has timed out.")
                {
                    throw new RpcRequestTimeoutException(webException.Message);
                }

                #endregion

                throw new RpcException("An unknown web exception occured while trying to read the JSON response", webException);
            }
            catch (JsonException jsonException)
            {
                throw new RpcResponseDeserializationException("There was a problem deserializing the response from the wallet", jsonException);
            }
            catch (ProtocolViolationException protocolViolationException)
            {
                throw new RpcException("Unable to connect to the server", protocolViolationException);
            }
            catch (RpcInternalServerErrorException rpcInternalServerErrorException)
            {
                throw new RpcInternalServerErrorException(rpcInternalServerErrorException.Message, rpcInternalServerErrorException);
            }
            catch (Exception exception)
            {
                var queryParameters = jsonRpcRequest.Parameters.Cast <string>().Aggregate(string.Empty, (current, parameter) => current + (parameter + " "));
                throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method} with parameters: {queryParameters}. \nException: {exception.Message}");
            }
        }
예제 #6
0
        public async Task <T> MakeRequestAsync <T>(RpcMethods rpcMethod, CancellationToken cancellationToken, params object[] parameters)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(_httpClient));
            }

            var jsonRpcRequest = new JsonRpcRequest(1, rpcMethod.ToString(), parameters);

            using (var request = new HttpRequestMessage(HttpMethod.Post, _coinService.Parameters.SelectedDaemonUrl))
            {
                var requestContent = jsonRpcRequest.GetBytes();

                request.Content = new ByteArrayContent(requestContent);
                request.Content.Headers.ContentType   = new MediaTypeHeaderValue("application/json-rpc");
                request.Content.Headers.ContentLength = requestContent.Length;

                try
                {
                    using (var response = await _httpClient.Value.SendAsync(request,
                                                                            HttpCompletionOption.ResponseContentRead, cancellationToken))
                    {
                        if (response.Content == null)
                        {
                            throw new RpcException(
                                      "The RPC request was either not understood by the server or there was a problem executing the request");
                        }

                        var responseContent = await response.Content.ReadAsStringAsync();

                        if (!response.IsSuccessStatusCode)
                        {
                            switch (response.StatusCode)
                            {
                            case HttpStatusCode.InternalServerError:
                                try
                                {
                                    var jsonRpcResponseObject =
                                        JsonConvert.DeserializeObject <JsonRpcResponse <object> >(responseContent);

                                    var internalServerErrorException =
                                        new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RpcException(responseContent);
                                }

                            default:
                                throw new RpcException(
                                          "The RPC request was either not understood by the server or there was a problem executing the request");
                            }
                        }

                        var rpcResponse = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(responseContent);
                        return(rpcResponse.Result);
                    }
                }
                catch (HttpRequestException httpRequestException)
                {
                    throw new RpcException(
                              "The RPC request was either not understood by the server or there was a problem executing the request",
                              httpRequestException);
                }
                catch (JsonException jsonException)
                {
                    throw new RpcResponseDeserializationException(
                              "There was a problem deserializing the response from the wallet", jsonException);
                }
                catch (TimeoutException)
                {
                    throw new RpcRequestTimeoutException("The operation has timed out");
                }
                catch (RpcInternalServerErrorException)
                {
                    throw;
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    var queryParameters = jsonRpcRequest.Parameters.Cast <string>().Aggregate(string.Empty, (current, parameter) => current + (parameter + " "));
                    throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method} with parameters: {queryParameters}. \nException: {exception.Message}");
                }
            }
        }
예제 #7
0
        public T MakeRequest <T>(Methods method, params object[] parameters)
        {
            var jsonRpcRequest = new JsonRpcRequest(1, method.ToString(), parameters);
            var webRequest     = (HttpWebRequest)WebRequest.Create(_url);

            SetBasicAuthHeader(webRequest, _user, _pwd);
            webRequest.Credentials = new NetworkCredential(_user, _pwd);
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method      = "POST";
            webRequest.Proxy       = null;
            webRequest.Timeout     = 3000;
            var byteArray = jsonRpcRequest.GetBytes();

            webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;

            using (var dataStream = webRequest.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Dispose();
            }

            try
            {
                string json;

                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }

                var rpcResponse = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(json);
                return(rpcResponse.Result);
            }
            catch (WebException webException)
            {
                var webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new Exception("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                var result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    var jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRpcResponse <object> >(result);

                                    //var internalServerErrorException = new Exception(jsonRpcResponseObject.Error.Message, webException)
                                    //{
                                    //    RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    //};

                                    //throw internalServerErrorException;
                                    throw new Exception(result, webException);
                                }
                                catch (JsonException)
                                {
                                    throw new Exception(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new Exception("Unhandled web exception", webException);
                    }
                }

                if (webException.Message == "The operation has timed out")
                {
                    throw new Exception(webException.Message);
                }

                throw new Exception("An unknown web exception occured while trying to read the response", webException);
            }
            catch (Exception ex)
            {
                throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method}", ex);
            }
        }
        public T MakeRequest <T>(RpcMethods rpcMethod, params object[] parameters)
        {
            id++;
            var jsonRpcRequest = new JsonRpcRequest(id, rpcMethod.ToString(), parameters);
            var webRequest     = (HttpWebRequest)WebRequest.Create(_coinService.Parameters.SelectedDaemonUrl);

            byte[] byteArray;

            if (rpcMethod == RpcMethods.stop) /* Dirty workaround to properly support json notification syntax, lets merge this into library later properly*/
            {
                var jsonRpcRequestNotification = new JsonRpcRequestNotification(rpcMethod.ToString(), parameters);
                SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
                webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);

                webRequest.ContentType = "application/json-rpc";
                webRequest.Method      = "POST";
                webRequest.Proxy       = null;
                webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
                byteArray = jsonRpcRequestNotification.GetBytes();
                webRequest.ContentLength = jsonRpcRequestNotification.GetBytes().Length;
            }
            else if (rpcMethod == RpcMethods.waitforchange) /* Latest XAYA library addition pases waitforchange arguments as array, this also needs special handling*/
            {
                string[] aRR = new string[parameters.Length];

                for (int s = 0; s < parameters.Length; s++)
                {
                    aRR[s] = parameters[s].ToString();
                }

                var jsonRpcRequestNotification = new JsonRpcRequestArray(id, rpcMethod.ToString(), aRR);
                SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
                webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);

                webRequest.ContentType = "application/json-rpc";
                webRequest.Method      = "POST";
                webRequest.Proxy       = null;
                webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
                byteArray = jsonRpcRequestNotification.GetBytes();
                webRequest.ContentLength = jsonRpcRequestNotification.GetBytes().Length;
            }
            else
            {
                SetBasicAuthHeader(webRequest, _coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);
                webRequest.Credentials = new NetworkCredential(_coinService.Parameters.RpcUsername, _coinService.Parameters.RpcPassword);

                webRequest.ContentType = "application/json-rpc";
                webRequest.Method      = "POST";
                webRequest.Proxy       = null;
                webRequest.Timeout     = _coinService.Parameters.RpcRequestTimeoutInSeconds * GlobalConstants.MillisecondsInASecond;
                byteArray = jsonRpcRequest.GetBytes();
                webRequest.ContentLength = jsonRpcRequest.GetBytes().Length;
            }

            if (rpcMethod == RpcMethods.waitforchange) /* Dirty workaround to properly support json notification syntax, lets merge this into library later properly*/
            {
                webRequest.Timeout = int.MaxValue;
            }

            try
            {
                using (var dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Dispose();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("There was a problem sending the request to the wallet", exception);
                return(default(T));
            }

            try
            {
                string json;

                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var result = reader.ReadToEnd();
                            reader.Dispose();
                            json = result;
                        }
                    }
                }

                if (json != "")
                {
                    var rpcResponse = JsonConvert.DeserializeObject <JsonRpcResponse <T> >(json);
                    return(rpcResponse.Result);
                }
                else
                {
                    return(default(T));
                }
            }
            catch (WebException webException)
            {
                #region RPC Internal Server Error (with an Error Code)

                var webResponse = webException.Response as HttpWebResponse;

                if (webResponse != null)
                {
                    switch (webResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:
                    {
                        using (var stream = webResponse.GetResponseStream())
                        {
                            if (stream == null)
                            {
                                throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                var result = reader.ReadToEnd();
                                reader.Dispose();

                                try
                                {
                                    var jsonRpcResponseObject = JsonConvert.DeserializeObject <JsonRpcResponse <object> >(result);

                                    var internalServerErrorException = new RpcInternalServerErrorException(jsonRpcResponseObject.Error.Message, webException)
                                    {
                                        RpcErrorCode = jsonRpcResponseObject.Error.Code
                                    };

                                    throw internalServerErrorException;
                                }
                                catch (JsonException)
                                {
                                    throw new RpcException(result, webException);
                                }
                            }
                        }
                    }

                    default:
                        throw new RpcException("The RPC request was either not understood by the server or there was a problem executing the request", webException);
                    }
                }

                #endregion

                #region RPC Time-Out

                if (webException.Message == "The operation has timed out")
                {
                    throw new RpcRequestTimeoutException(webException.Message);
                }

                #endregion

                return(default(T));
            }
            catch (JsonException jsonException)
            {
                throw new RpcResponseDeserializationException("There was a problem deserializing the response from the wallet", jsonException);
            }
            catch (ProtocolViolationException protocolViolationException)
            {
                throw new RpcException("Unable to connect to the server", protocolViolationException);
            }
            catch (Exception exception)
            {
                var queryParameters = jsonRpcRequest.Parameters.Cast <string>().Aggregate(string.Empty, (current, parameter) => current + (parameter + " "));
                throw new Exception($"A problem was encountered while calling MakeRpcRequest() for: {jsonRpcRequest.Method} with parameters: {queryParameters}. \nException: {exception.Message}");
            }
        }