Can't perform remote JSON RPC to my Bitcoin node (C#)

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.

Johnny

Posted 2019-06-25T19:29:33.833

Reputation: 27

Answers

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

Pieter Wuille

Posted 2019-06-25T19:29:33.833

Reputation: 54 032

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 2019-06-26T03:01:48.923

The getinfo command was deprecated in version 0.16, and subsequently removed. Use the more specific getblockchaininfo, getpeerinfo, getnetworkinfo, getwalletinfo, ... instead.Pieter Wuille 2019-06-26T03:09:33.920

Oh yeah, I figured that out. The error is the same for any command. The error message is "Parse error".Johnny 2019-06-26T03:14:00.640

Can you update your question with the exact command and output?Pieter Wuille 2019-06-26T03:15:06.510

I tried your command on my system and it works fine.Pieter Wuille 2019-06-26T21:37:50.580