5

How do I access the JSON RPC API in C# in such a way that I can also read the errors that the interface provides?

2 Answers 2

4

I use the Newtonsoft.Json.Linq library as shown below to get the data. Soon, I'll be posting Strongly typed C# client libraries at http://www.coinapi.net

The main trick to getting the JSON error code is to call GetWebResponse() again in the catch method. Then return and parse the data. I've debated about including the WebException itself in the return code, but that may require too many changes. If anyone has ideas I'll be glad to take them.

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

Here is the definition of 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;
        }
    }
1

You might also want to take a look at this C# bitcoin library: https://github.com/GeorgeKimionis/BitcoinLib

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.