1

I'm trying to perform a remote JSON RPC. It works fine using localhost but not if I try to do it from another computer.

Here's what I have so far:

            int index = 0;
        HttpWebRequest webRequest = (HttpWebRequest).Create("http://fakeip:8332");

        webRequest.Credentials = new NetworkCredential("fakeuser", "fakepw");
        /// 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"));
        joe.Add(new JProperty("id", "1"));
        joe.Add(new JProperty("method", "getinfo"));
        // params is a collection values which the method requires..
        Dictionary<string, string> Params = new Dictionary<string, string>();
        if (Params.Keys.Count == 0) {
            joe.Add(new JProperty("params", new JArray()));
        } else {
            JArray props = new JArray();
            // add the props in the reverse order!
            for (int i = Params.Keys.Count - 1; i >= 0; i--) {
            }
            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();

        WebResponse webResponse = webRequest.GetResponse();

which gives this exception:

    Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 
A connection attempt failed because the connected party did not properly respond 
after a period of time, or established connection failed because connected host
 has failed to respond fakeip:8332

I've tried using curl too:

   curl --data-binary 
    '{"jsonrpc":"1.0","id":"curltext","method":"getnetworkinfo","params":[]}' 
-H 'content-type:text/plain;' http://fakeuser:fakepw@fakeip:8332

curl: (7) Failed to connect to fakeip port 8332: Timed out

My bitcoin.conf:

testnet=0
server=1
rpcuser=fakeuser
rpcpassword=fakepw
rpcallowip=0.0.0.0/0
txindex=1

Anyone have any ideas?

Edit: rpcbind allows me to make the RPC query using C# but the curl method is still not working.

Running the following:

curl --data-binary 
'{"jsonrpc":"1.0","id":"curltext","method":"getnetworkinfo","params":[]}' -H
 'content-type:text/plain;' http://fakeuser:fakepw@fakeip:8332

results in:

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

I get the same error for any command.

1

You'll need to specify an IP address to bind the RPC interface to. Since Bitcoin Core 0.18, it's not sufficient to have "rpcallowip=*", as it will by still default still only bind to localhost.

So add:

rpcbind=fakeip
  • Hi Pieter, big fan. The C# code is working now but I'm getting error code -32700 when using curl. Any idea why? If not, using C# should be enough for what I want to do. Thanks a lot. – Johnny Jun 26 at 3:01
  • The getinfo command was deprecated in version 0.16, and subsequently removed. Use the more specific getblockchaininfo, getpeerinfo, getnetworkinfo, getwalletinfo, ... instead. – Pieter Wuille Jun 26 at 3:09
  • Oh yeah, I figured that out. The error is the same for any command. The error message is "Parse error". – Johnny Jun 26 at 3:14
  • Can you update your question with the exact command and output? – Pieter Wuille Jun 26 at 3:15
  • I tried your command on my system and it works fine. – Pieter Wuille Jun 26 at 21:37

Your Answer

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

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