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\oshirowanen\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\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>

Anyone know why that is?

  • 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? – Andrew Chow Dec 2 at 17:46
  • @AndrewChow I've added the full error message. – oshirowanen Dec 3 at 19:51
  • 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. – Andrew Chow Dec 3 at 20:36
up vote 1 down vote accepted

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

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>() { }));
  • 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 Dec 3 at 19:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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