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?