2

As per this code sample, I'm attempting to make a .net httpwebrequest from my local machine to a local bitcoind -daemon server. However, I'm only receiving a 400 bad request at webRequest.GetResponse().

I am completely stumped as to how to proceed - is there anything obviously wrong with this code? I have the bitcoin.conf file set up with the below username and pass, and it is set to port 8332. These are the ONLY settings I've set in the bitcoin.conf. I'm a noobie in both web requests and the Bitcoin api, so I suspect it's something stupidly obvious.

For a little more information, when I manually navigate to localhost:8332 in the browser, the credentials I use in my program work, and the web page returns:

{"result":null,"error":{"code":-32700,"message":"Parse error"},"id":null}

Here is the code that makes the request:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8332");
webRequest.Credentials = new NetworkCredential("blah", "blahblah");
/// important, otherwise the service can't desirialse your request properly
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";

JObject joe = new JObject();
joe.Add(new JProperty("jsonrpc", "1.0"));

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

WebResponse webResponse = webRequest.GetResponse();
  • 1
    I'm reading the bitcoin source, and there are a few things that can cause the rpc service to come back with error 400. Could you post the full response text that your program gets? – Nick ODell Apr 1 '13 at 8:27
  • As far as I can see, you aren't actually calling a method. Duplicating your request as best I can with curl (curl --user user --data-binary '{"jsonrpc": "1.0"}' 127.0.0.1:8332) gets me a "Missing method" error, which could be your problem, though I'm not remotely familiar with the Json.Net library. – BinaryMage Apr 2 '13 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.