Communicate with Bitcoin-qt using C# / .NET

Code used in video

private static void Main(string[] args)
var data = RequestServer(“getblockcount”, new List<string>() { “value”});

public static string RequestServer(string methodName, List<string> parameters)

string ServerIp = “http://localhost.:8332”;
string UserName = “BtcMyFineUserForTesting”;
string Password = “sdfaf5143gEAD”;

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)

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

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

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

respVal = streamReader.ReadToEnd();
var data = JsonConvert.DeserializeObject(respVal).ToString();
return data;
catch (Exception exp)

if (streamReader != null)

return string.Empty;



2 thoughts on “Communicate with Bitcoin-qt using C# / .NET”

  1. Hi there,

    Like your work.

    What version of Visual Studio have you done this in ?

    I have vs2008 and have a small problem.

    Seems that I have to use . . .
    using System.Runtime.Serialization.Json;

    According to some MSDN comment ….
    In net 3.5, reference System.ServiceModel.Web and find Json in System.Runtime.Serialization.Json; works for VS 2008.

    Did that, so I get . . .

    Error 1 The type or namespace name ‘JObject’ could not be found (are you missing a using directive or an assembly reference?) C:\Users\allan\Documents\Visual Studio 2008\Projects\Bitcoin001\Bitcoin001\Form1.cs 60 1 Bitcoin001

    Some other errors related to the Json

    Is it easier to update my Visual Studio perhaps?

  2. I am reviewing your videos – good material.

    How may i specify regtest-mode when using your wrapper?

Leave a Reply

Your email address will not be published. Required fields are marked *