<p>The issue is that you are sending the <code>1000</code> parameter as a string, not as a number. The JSON type when the RPC server receives it will be a string, which is incorrect. It does not convert this to an integer implicitly so you need to explicitly set the type. So instead you should be doing:</p>

<pre><code>JObject data = JObject.Parse(RequestServer("listunspent", new List&lt;Integer&gt;() { 1000 }));
</code></pre>

CC BY-SA 4.0
2018-12-03T20:38:11.380Z
2018-12-03T20:38:11.380Z
Ava Chow
<p>maybe your over complicating it</p>

<p>just call:</p>

<pre><code>requestserver("listunspent 1000")    
</code></pre>

<p>thus it sends out "listunspent 1000"    </p>

<pre><code>JObject data = JObject.Parse(RequestServer("listunspent 1000", new List&lt;string&gt;() { }));
</code></pre>

CC BY-SA 4.0
2018-12-03T19:52:29.177Z
That gives the following error message: Unhandled Exception: System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at ConsoleApp1.Program.RequestServer(String methodName, List`1 parameters) in C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 63 at ConsoleApp1.Program.Main(String[] args) in C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 18
oshirowanen
CC BY-SA 4.0
2018-12-03T05:25:51.433Z
2018-12-03T06:23:02.447Z
2018-12-03T06:23:02.447Z
Michael Hunt
<p>I am trying to use the following example code for C# programming, so I can connect to my own bitcoin full node:</p>

<p><a href="https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29</a></p>

<p>Which has resulted in something like this:</p>

<pre><code>public static string RequestServer(string methodName, List&lt;string&gt; parameters)
{
    string ServerIp = "http://localhost:18332";
    string UserName = "user name goes here";
    string Password = "password goes here";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIp);
    webRequest.Credentials = new NetworkCredential(UserName, Password);

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

    string respVal = string.Empty;

    JObject joe = new JObject();
    joe.Add(new JProperty("jsonrpc", "1.0"));
    joe.Add(new JProperty("id", "1"));
    joe.Add(new JProperty("method", methodName));

    JArray props = new JArray();
    foreach (var parameter in parameters)
    {
        props.Add(parameter);
    }

    joe.Add(new JProperty("params", props));

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

    Stream dataStream = webRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    StreamReader streamReader = null;
    try
    {
        WebResponse webResponse = webRequest.GetResponse();

        streamReader = new StreamReader(webResponse.GetResponseStream(), true);

        respVal = streamReader.ReadToEnd();
        var data = JsonConvert.DeserializeObject(respVal).ToString();
        return data;
    }
    catch (Exception exp)
    {
        throw (exp);
    }
    finally
    {
        if (streamReader != null)
        {
            streamReader.Close();
        }
    }

    return string.Empty;
}
</code></pre>

<p>The above code allows me to run the bitcoin command <code>sendtoaddress</code>.  For example, the following would work in the bitcoin console:</p>

<p><code>senttoaddress 2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF 0.01</code></p>

<p>Using the <code>RequestServer</code> function in the C# code above, I can do the same thing like this:</p>

<pre><code>double AmountToSend = Convert.ToDouble(txtAmountToSend.Text);
string ReceivingAddress = txtSendToAddress.Text;
JObject data = JObject.Parse(RequestServer("sendtoaddress", new List&lt;string&gt;() { ReceivingAddress, Convert.ToString(AmountToSend) }));
</code></pre>

<p>All good so far.</p>

<p>Now I want to do the same with <code>listunspent</code></p>

<p>In the bitcoin console, the following work examples work fine:</p>

<pre><code>listunspent
listunspent 1000
</code></pre>

<p>Using the C# function above, the following works if I do not give it a parameter:</p>

<p><code>JObject data = JObject.Parse(RequestServer("listunspent", new List&lt;string&gt;() { }));</code></p>

<p>But when I add a parameter, it does not work:</p>

<p><code>JObject data = JObject.Parse(RequestServer("listunspent", new List&lt;string&gt;() { Convert.ToString(1000) }));</code></p>

<p>The error it gives is:</p>

<pre><code>C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug&gt;ConsoleApp1.exe

Unhandled Exception: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleApp1.Program.RequestServer(String methodName, List`1 parameters) in C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 63
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 18

C:\Users\oshirowanen\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug&gt;
</code></pre>

<p>Anyone know why that is?</p>

CC BY-SA 4.0
2018-12-02T17:46:30.443Z
The error you get should come with some message of what the error is, not just the error code. It is likely that you are just setting the parameters incorrectly. Can you find the error message and include that your question as well?
Ava Chow
CC BY-SA 4.0
2018-12-03T19:51:51.673Z
@AndrewChow I've added the full error message.
oshirowanen
CC BY-SA 4.0
2018-12-03T20:36:36.550Z
I meant the body of the HTTP response which has the 500 error, not the stack trace. However, I think I know what the problem is.
Ava Chow
<bitcoin-core><json-rpc><bitcoin-core-development><c#>
Invoking the listunspent command via json-rpc on a full node
oshirowanen
# Invoking the listunspent command via json-rpc on a full node

(Asked by: username_0 on 2018-12-02)

I am trying to use the following example code for C# programming, so I can connect to my own bitcoin full node:

<https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29>

Which has resulted in something like this:

```
public static string RequestServer(string methodName, List<string> parameters)
{
    string ServerIp = "http://localhost:18332";
    string UserName = "user name goes here";
    string Password = "password goes here";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIp);
    webRequest.Credentials = new NetworkCredential(UserName, Password);

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

    string respVal = string.Empty;

    JObject joe = new JObject();
    joe.Add(new JProperty("jsonrpc", "1.0"));
    joe.Add(new JProperty("id", "1"));
    joe.Add(new JProperty("method", methodName));

    JArray props = new JArray();
    foreach (var parameter in parameters)
    {
        props.Add(parameter);
    }

    joe.Add(new JProperty("params", props));

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

    Stream dataStream = webRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    StreamReader streamReader = null;
    try
    {
        WebResponse webResponse = webRequest.GetResponse();

        streamReader = new StreamReader(webResponse.GetResponseStream(), true);

        respVal = streamReader.ReadToEnd();
        var data = JsonConvert.DeserializeObject(respVal).ToString();
        return data;
    }
    catch (Exception exp)
    {
        throw (exp);
    }
    finally
    {
        if (streamReader != null)
        {
            streamReader.Close();
        }
    }

    return string.Empty;
}

```

The above code allows me to run the bitcoin command `sendtoaddress`. For example, the following would work in the bitcoin console:

`senttoaddress 2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF 0.01`

Using the `RequestServer` function in the C# code above, I can do the same thing like this:

```
double AmountToSend = Convert.ToDouble(txtAmountToSend.Text);
string ReceivingAddress = txtSendToAddress.Text;
JObject data = JObject.Parse(RequestServer("sendtoaddress", new List<string>() { ReceivingAddress, Convert.ToString(AmountToSend) }));

```

All good so far.

Now I want to do the same with `listunspent`

In the bitcoin console, the following work examples work fine:

```
listunspent
listunspent 1000

```

Using the C# function above, the following works if I do not give it a parameter:

`JObject data = JObject.Parse(RequestServer("listunspent", new List<string>() { }));`

But when I add a parameter, it does not work:

`JObject data = JObject.Parse(RequestServer("listunspent", new List<string>() { Convert.ToString(1000) }));`

The error it gives is:

```
C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug>ConsoleApp1.exe

Unhandled Exception: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleApp1.Program.RequestServer(String methodName, List`1 parameters) in C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 63
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 18

C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug>

```

Anyone know why that is?

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

> **Comments:**
> **username_1**: The error you get should come with some message of what the error is, not just the error code. It is likely that you are just setting the parameters incorrectly. Can you find the error message and include that your question as well?  
> **username_0**: @AndrewChow I've added the full error message.  
> **username_1**: I meant the body of the HTTP response which has the 500 error, not the stack trace. However, I think I know what the problem is.  

---

## 2 answers

### Answer by username_1 on 2018-12-03. Score: 1 | Accepted answer

The issue is that you are sending the `1000` parameter as a string, not as a number. The JSON type when the RPC server receives it will be a string, which is incorrect. It does not convert this to an integer implicitly so you need to explicitly set the type. So instead you should be doing:

```
JObject data = JObject.Parse(RequestServer("listunspent", new List<Integer>() { 1000 }));

```


---

### Answer by username_2 on 2018-12-03. Score: 0

maybe your over complicating it

just call:

```
requestserver("listunspent 1000")    

```

thus it sends out "listunspent 1000"

```
JObject data = JObject.Parse(RequestServer("listunspent 1000", new List<string>() { }));

```

> **Comments:**
> **username_0**: That gives the following error message: Unhandled Exception: System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at ConsoleApp1.Program.RequestServer(String methodName, List`1 parameters) in C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 63 at ConsoleApp1.Program.Main(String[] args) in C:\Users\username_0\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 18  

---

