0

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?

3
  • 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
    Commented Dec 2, 2018 at 17:46
  • @AndrewChow I've added the full error message. Commented Dec 3, 2018 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.
    – Ava Chow
    Commented Dec 3, 2018 at 20:36

2 Answers 2

1

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

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.