无法使用 JSON-RPC 连接到比特币钱包



我正在尝试从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

如果这些是你的代码和 .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中 JSONRPC 2.0 的底部.java大约在第 495 行附近。

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

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

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

最新更新