¿Cómo llamo a la API JSON RPC usando C#?

¿Cómo accedo a la API JSON RPC en C# de tal manera que también pueda leer los errores que proporciona la interfaz?

Respuestas (2)

Utilizo la Newtonsoft.Json.Linqbiblioteca como se muestra a continuación para obtener los datos. Pronto, estaré publicando bibliotecas cliente de C# fuertemente tipadas en http://www.coinapi.net

El truco principal para obtener el código de error JSON es llamar a GetWebResponse() nuevamente en el método catch. Luego regrese y analice los datos. He debatido sobre la inclusión de WebException en el código de retorno, pero eso puede requerir demasiados cambios. Si alguien tiene ideas estaré encantada de tomarlas.

        var ret = InvokeMethod("getblockhash", index);

Aquí está la definición de InvokeMethod

    public JObject InvokeMethod(string a_sMethod, params object[] a_params)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
        webRequest.Credentials = Credentials;

        webRequest.ContentType = "application/json-rpc";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "1.0";
        joe["id"] = "1";
        joe["method"] = a_sMethod;

        if (a_params != null)
        {
            if (a_params.Length > 0)
            {
                JArray props = new JArray();
                foreach (var p in a_params)
                {
                    props.Add(p);
                }
                joe.Add(new JProperty("params", props));
            }
        }

        string s = JsonConvert.SerializeObject(joe);
        // serialize json for the request
        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        webRequest.ContentLength = byteArray.Length;

        try
        {
            using (Stream dataStream = webRequest.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
        }
        catch (WebException we)
        {
            //inner exception is socket
            //{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 23.23.246.5:8332"}
            throw;
        }
        WebResponse webResponse = null;
        try
        {
            using (webResponse = webRequest.GetResponse())
            {
                using (Stream str = webResponse.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(str))
                    {
                        return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
                    }
                }
            }
        }
        catch (WebException webex)
        {

            using (Stream str = webex.Response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(str))
                {
                    var tempRet =  JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
                    return tempRet;
                }
            }

        } 
        catch (Exception)
        {

            throw;
        }
    }

También puede echar un vistazo a esta biblioteca de bitcoin C#: https://github.com/GeorgeKimionis/BitcoinLib