无法通过JSON-RPC连接到比特币钱包

时间:2014-03-02 10:17:28

标签: java json bitcoind

我正在尝试从Java连接到比特币钱包。但我得到网络异常:服务器重定向次数太多。如果有人帮我理解,我会很高兴。 这是我的代码:

public static void SetupRPC() {
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication ("admin", "admin".toCharArray());
        }
    });

    URL serverURL = null;
    try {
        serverURL = new URL("http://127.0.0.1:44843");
    } catch (MalformedURLException e) {
        System.err.println(e.getMessage());
    }

    JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
    String method = "getinfo";
    int requestID = 0;
    JSONRPC2Request request = new JSONRPC2Request(method, requestID);

    // Send request
    JSONRPC2Response response = null;

    try {
        response = mySession.send(request);
    } catch (JSONRPC2SessionException e) {
        System.err.println(e.getMessage());
    }

    if (response.indicatesSuccess())
        System.out.println(response.getResult());
    else
        System.out.println(response.getError().getMessage());
}

和.conf文件:

rpcuser="admin"
rpcpassword="admin"
rpcallowip=*
rpcport=44843
server=1
daemon=1
listen=1
rpcconnect=127.0.0.1

2 个答案:

答案 0 :(得分:0)

如果这些是您的代码和.conf,请删除conf文件中的"。或者将\"添加到您的字符串中。 所以这个

return new PasswordAuthentication ("\"admin\"", "\"admin\"".toCharArray());

或者

rpcuser="admin"
rpcpassword="admin"

此外,您的密码或用户名不是#(我的情况也是如此)。

在我发现这个后,我得到了Invalid JSON-RPC 2.0 response

我的解决方案是在函数中将!jsonObject.containsKey("error")更改为(!jsonObject.containsKey("error") || jsonObject.get("error") == null) public JSONRPC2Response parseJSONRPC2Response(final String jsonString)位于JSONRPC2Parser.java中JSONRPC 2.0的基础,位于495行附近。

编辑:这是在DogeCoin-QT上,所以也许Bitcoin-QT没有这个问题。

答案 1 :(得分:0)

好的,我解决了这个问题:

Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "pass".toCharArray());
        }
    });
    String uri = "http://127.0.0.1:8332";
    String requestBody = "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"getbalance\"}";
    String contentType = "application/json";
    HttpURLConnection connection = null;
    try {
        URL url = new URL(uri);
        connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", contentType);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
        connection.setUseCaches(true);
        connection.setDoInput(true);
        OutputStream out = connection.getOutputStream();
        out.write(requestBody.getBytes());
        out.flush();
        out.close();
    } catch (IOException ioE) {
        connection.disconnect();
        ioE.printStackTrace();
    }

    try {
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            System.out.println(response);
        } else {
            connection.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }