Json-Rpc

無法對我的比特幣節點執行遠端 JSON RPC (C#)

  • June 26, 2019

我正在嘗試執行遠端 JSON RPC。使用 localhost 可以正常工作,但如果我嘗試從另一台電腦執行此操作,則無法正常工作。

這是我到目前為止所擁有的:

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

這給出了這個例外:

   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

我也嘗試過使用 curl:

  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

我的 bitcoin.conf:

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

誰有想法?

編輯:rpcbind 允許我使用 C# 進行 RPC 查詢,但 curl 方法仍然不起作用。

執行以下:

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

結果是:

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

對於任何命令,我都會收到相同的錯誤。

您需要指定一個 IP 地址來綁定 RPC 介面。從比特幣核心 0.18 開始,只有“rpcallowip=*”是不夠的,因為它仍然預設仍然只綁定到本地主機。

所以添加:

rpcbind=fakeip

引用自:https://bitcoin.stackexchange.com/questions/88648