<p>I use the <code>Newtonsoft.Json.Linq</code> library as shown below to get the data.  Soon, I'll be posting Strongly typed C# client libraries at <a href="http://www.coinapi.net" rel="noreferrer">http://www.coinapi.net</a> </p>

<p>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.</p>

<pre><code>        var ret = InvokeMethod("getblockhash", index);
</code></pre>

<p>Here is the definition of InvokeMethod</p>

<pre><code>    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 &gt; 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&lt;JObject&gt;(sr.ReadToEnd());
                    }
                }
            }
        }
        catch (WebException webex)
        {

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

        } 
        catch (Exception)
        {

            throw;
        }
    }
</code></pre>

CC BY-SA 3.0
2012-12-16T21:22:57.720Z
2012-12-16T21:22:57.720Z
makerofthings7
<p>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?</p>

<development><json-rpc><c#>
How do I call JSON RPC API using C#?
makerofthings7
# How do I call JSON RPC API using C#?

(Asked by: username_0 on 2012-12-16)

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?

**Tags:** development, json-rpc, c#


---

## 1 answers

### Answer by username_0 on 2012-12-16. Score: 4 | Accepted answer

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;
        }
    }

```


---

