Exemplo n.º 1
0
        /// <summary>
        /// Gets the JSON response for the given HTTP request.
        /// </summary>
        /// <param name="httpWebRequest">The HTTP request send to the Bitcoin RPC interface.</param>
        /// <returns>The raw JSON string.</returns>
        private string GetJsonResponse(HttpWebRequest httpWebRequest)
        {
            try
            {
                WebResponse webResponse = httpWebRequest.GetResponse();

                // Deserialize the json response
                using (var stream = webResponse.GetResponseStream())
                {
                    if (stream == null)
                    {
                        return(string.Empty);
                    }

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

                        return(result);
                    }
                }
            }
            catch (ProtocolViolationException protocolException)
            {
                throw _rpcExceptionFactory.GetRpcException(protocolException);
            }
            catch (WebException webException)
            {
                var response = webException.Response as HttpWebResponse;

                if (response == null)
                {
                    throw _rpcExceptionFactory.GetRpcException("Error while reading json response", webException);
                }

                var error = ReadJsonError(response); // try to read the error response.

                if (error != null)
                {
                    throw _rpcExceptionFactory.GetRpcErrorException(error); //throw the error.
                }
                else
                {
                    throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", webException);
                }
            }
            catch (Exception exception)
            {
                throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", exception);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the JSON response for the given HTTP request.
        /// </summary>
        /// <param name="httpWebRequest">The HTTP request send to the Bitcoin RPC interface.</param>
        /// <returns>The raw JSON string.</returns>
        private string GetJsonResponse(HttpWebRequest httpWebRequest)
        {
            WebResponse webResponse = null;

            try
            {
                using (webResponse = httpWebRequest.GetResponse())
                {
                    // Deserialize the json response
                    using (var stream = webResponse.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            return(string.Empty);
                        }

                        using (var reader = new StreamReader(stream))
                        {
                            return(reader.ReadToEnd());
                        }
                    }
                }
            }
            catch (ProtocolViolationException protocolException)
            {
                _logger.Error(protocolException.Message);
                throw _rpcExceptionFactory.GetRpcException(protocolException);
            }
            catch (WebException webException)
            {
                _logger.Error(webException.Message);
                var response = webException.Response as HttpWebResponse;

                if (response == null)
                {
                    throw _rpcExceptionFactory.GetRpcException(webException);
                }

                var error = ReadJsonError(response); // try to read the error response.

                if (error != null)
                {
                    throw _rpcExceptionFactory.GetRpcErrorException(error); //throw the error.
                }
                else
                {
                    throw _rpcExceptionFactory.GetRpcException(
                              "An unknown exception occured while reading json response.", webException);
                }
            }
            catch (Exception exception)
            {
                _logger.Error(exception.Message);
                throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.",
                                                           exception);
            }
            finally
            {
                if (webResponse != null)
                {
                    webResponse.Close(); //Close webresponse connection
                }
                webResponse = null;      //To clear up the webresponse
            }
        }