0

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

2 回答 2

0

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

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

或这个

rpcuser="admin"
rpcpassword="admin"

此外,您的密码或用户名中不能有 #(这是我的情况)。

在我找到这个之后,我得到了一个Invalid JSON-RPC 2.0 response.

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

编辑:这是在 DogeCoin-QT 上,所以比特币-QT 可能没有这个问题。

于 2014-03-03T03:10:50.183 回答
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();
    }
于 2014-03-03T11:31:37.477 回答